I am using concat() to create the name for a column name:
set #var = 10;
select col as concat("str1", cast(#var as CHAR), "str2") from table1;
But I get the following syntax error:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ('str1', cast(#var as char), 'str2')
What am I doing wrong?
Try:
SET #var := 10;
SET #`sql` := CONCAT('select col as ',
'str1', cast(#var as CHAR), 'str2',
' from table1');
PREPARE `stmt` FROM #`sql`;
EXECUTE `stmt`;
DEALLOCATE PREPARE `stmt`;
Related
I want to verify if an index exists for a SQL table. I'm trying this command (from here) :
SELECT *
FROM sys.indexes
WHERE name='YourIndexName' AND object_id = OBJECT_ID('Schema.YourTableName')
But I'm getting the error :
OperationalError: no such table: sys.indexes
with sqlite3 in Python.
SELECT
*
FROM
sqlite_master
WHERE
type= 'index' and tbl_name = 'your_table_name' and name = 'your_index_name';
You can use above query to verify index exist for a specific table and specific name.
What I want to do is check in my database if my table exists, if yes drop it. Here is my .tpt :
DEFINE JOB DELETE_ET_TABLES
DESCRIPTION 'Delete ET tables'
(
DEFINE OPERATOR DDL_OPERATOR
DESCRIPTION 'Teradata Parallel Transporter DDL Operator'
TYPE DDL
ATTRIBUTES
(
varchar TdpId = #TERADATA_TDP,
varchar UserName = #User,
varchar UserPassword = #Pwd
);
APPLY
'SELECT (CASE WHEN TableName = ''Test_Del''
THEN (''DROP TABLE #Table;'')
ELSE NULL
END)
FROM dbc.TablesV WHERE databasename = #Db;' TO OPERATOR(DDL_OPERATOR);
And this is the error message I am getting :
Running "tbuild" command: tbuild -f /$HOME/loaders/test_deleteETTables.tpt -u TERADATA_TDP=$TDP, TERADATA_DATABASE=$DB -L /$LOG/
Teradata Parallel Transporter Version 16.20.00.09 64-Bit
TPT_INFRA: Syntax error at or near line 18 of Job Script File '/$HOME/loaders/test_deleteETTables.tpt':
TPT_INFRA: At "(" missing { EXTENDED_LITERAL_ CHAR_STRING_LITERAL_ } in Rule: Character String Literal
Compilation failed due to errors. Execution Plan was not generated.
Do you have any idea ? I have tried multiple things, such as :
SELECT 1 FROM dbc.TablesV WHERE databasename = #Db AND TABLENAME ='TEST_DEL';
CASE WHEN ACTIVITYCOUNT = 1
THEN (DROP TABLE #Table)
ELSE ( QUIT )
END;
All my variables have been declared. I feel that it is a problem with using single quotes inside que statement but I am not sure and I don't know how to resolve it. Thank you very much for your time.
The solution that Fred recommended me to try in the comments worked just fine :
I think this is due to use of NULL but SELECT is not valid for DDL operator. The recommended way to do this is simply pass a DROP to the operator and tell it to ignore "not found" (and consider that success), i.e. ErrorList='3807'
DESCRIPTION 'Delete ET tables'
(
DEFINE OPERATOR DDL_OPERATOR
DESCRIPTION 'Teradata Parallel Transporter DDL Operator'
TYPE DDL
ATTRIBUTES
(
varchar TdpId = #TERADATA_TDP,
varchar UserName = #USERDB,
varchar UserPassword = #PWD,
VARCHAR ErrorList = '3807'
);
APPLY
('DROP TABLE #TABLENAME')
TO OPERATOR(DDL_OPERATOR);
);```
sqlite> create table t
...> SELECT * FROM sqlite_master WHERE type='table' AND name='t';
Error: near "SELECT": syntax error
sqlite>
I'm trying to create a table called 't' in terminal. I'm getting the error. Maybe I'm missing somewhere?
The syntax you need to use here is CREATE TABLE AS ... SELECT:
> CREATE TABLE t AS SELECT * FROM sqlite_master WHERE type = 'table' AND name = 't';
I attempt to create a procedure or function to populate a table name Time_Dim in MariaDB.
DELIMITER /
CREATE FUNCTION filltimeDim ()
BEGIN
DECLARE vQuarter VARCHAR(6);
DECLARE vMonth_Name VARCHAR(20);
DECLARE vdate_id date;
DECLARE vyear_id CHAR(4);
DECLARE vweekly_Name VARCHAR(20);
DECLARE vMonth_Num TINYINT(10);
DECLARE vweekday_Num TINYINT(10);
BEGIN
SET vdate_id = CONVERT('1998-01-01', DATE);
WHILE (CONVERT('vdate_id' USING utf8) <= '2002-12-31')
LOOP
SET vyear_id = YEAR(vdate_id);
SET vQuarter = QUARTER(vdate_id);
SET vMonth_Name = MONTHNAME(vdate_id);
SET vweekly_Name = DAYOFWEEK(vdate_id);
SET vMonth_Num = MONTH(vdate_id);
SET vweekday_Num = WEEKDAY(vdate_id);
INSERT INTO Time_Dim VALUES
(vdate_id, vyear_id, vquarter, vMonth_Name,vweekly_Name,vMonth_Num,vweekday_Num);
SET vdate_id = vdate_id + 1;
END LOOP;
END;
END; /
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'LOOP
SET vyear_id = YEAR(vdate_id);
SET vQuarter = QUA' at line 13
The syntax for while in MariaDB is:
[begin_label:] WHILE search_condition DO
statement_list
END WHILE [end_label]
https://mariadb.com/kb/en/library/while/
You shouldn't use the 'LOOP' keywords
You can use Sequence Storage Engine:
-- SELECT
-- DATEDIFF('2002-12-31', '1990-01-01'); -- 4747
INSERT INTO `Time_Dim` (
`vdate_id`,
`vyear_id`,
`vquarter`,
`vMonth_Name`,
`vweekly_Name`,
`vMonth_Num`,
`vweekday_Num`
)
SELECT
`der`.`day`,
YEAR(`der`.`day`),
QUARTER(`der`.`day`),
MONTHNAME(`der`.`day`),
DAYOFWEEK(`der`.`day`),
MONTH(`der`.`day`),
WEEKDAY(`der`.`day`)
FROM (
SELECT
'1990-01-01' + INTERVAL (`seq`) DAY `day`
FROM
`seq_0_to_4747`
) `der`;
See dbfiddle.
Is it possible to use SELECT FROM when using an associative array? I'm passing an array to a stored procedure through a .NET application, and I wanna be able to use that array as a condition when selecting from another table. Lets say I'm passing an array of IDs to the procedure, I wanna be able to do this:
select * from table1 where userID in (select column_value from array)
The type for the array is defined in the package:
type id_array is type of number index by pls_integer
Yes, it is possible, by wrapping the array with a pipelined function. Here's a good primer on pipelined functions:
http://www.oracle-developer.net/display.php?id=429
UPDATE: Oracle 12c now supports querying associative arrays using the TABLE operator, as long as the type is declared in a package spec: https://galobalda.wordpress.com/2014/08/02/new-in-oracle-12c-querying-an-associative-array-in-plsql-programs/
e.g.
select * from table1
where userID in (select column_value from table(array));
No, you can't select from PL/SQL arrays, since you use SQL in select from statements, though you can use DB defined Nested Tables types in SQL. This short article can help you get started.
Take a look a this simple synthetic exmple:
> create type temp_t as table of int;/
Type created.
> select 'test' from dual where 1 in (select * from table(temp_t(1,2,3)));
'TES
----
test
An example using PLSQL (to select from a nested table):
create type temp_r as OBJECT(
temp_varchar2 varchar2(100),
temp_number number(20)
);
/
create type temp_t as TABLE of temp_r;
/
set serveroutput on size 1000000
/
-- PLSQL starts here
declare
temp_rec temp_r := temp_r(null, null); -- empty constructor to initialize object
temp_table temp_t := temp_t(); -- empty constructor to initialize object
lv_ref_cursor SYS_REFCURSOR;
lv_temp_varchar2 varchar(100);
lv_temp_number number(20);
begin
temp_rec.temp_varchar2 := 'first';
temp_rec.temp_number := 1;
temp_table.extend;
temp_table(1) := temp_rec;
temp_table.extend;
temp_table(2) := temp_r('second', 2);
OPEN lv_ref_cursor FOR
SELECT temp_varchar2, temp_number
FROM table(temp_table)
where temp_number = 1;
fetch lv_ref_cursor into lv_temp_varchar2, lv_temp_number;
close lv_ref_cursor;
dbms_output.put_line('returns: ' || lv_temp_varchar2 || ', ' || lv_temp_number);
end;
/