Executing DDL in compound SQL using DashDB (DB2) - sql

I need to execute a DDL command (CREATE TABLE) with other SQL commands. See the code snippet below:
CREATE TABLE test AS
(
SELECT duration AS NUM1
FROM event
WHERE duration IS NOT NULL
) WITH NO DATA;
INSERT INTO test (
SELECT duration AS NUM1
FROM event
WHERE event_duration_tech IS NOT NULL
);
I am creating a table, then populating it.
If I send this code via JDBC, it does not work due to a statement terminator (;) error.
If I wrap it with BEGIN and END to create a compound SQL block, it does not work because DB2 does not allow DDL commands on compound SQL blocks.
The thing is, I need to execute both commands in one shot. Any ideas?

You need to use dynamic SQL to execute some DDL statements:
EXECUTE IMMEDIATE 'CREATE TABLE test AS (SELECT...'

Related

How to drop an existing index in Firebird?

What a script should I create to check is this index exist? Because I want this index will be dropped if it has been created yet and then create the index again
CREATE INDEX IF NOT EXISTS IDX_TABLE ON TABLE (ID, DATE)
I need only one script to make it automatically. I have not found an alternative for with drop_existing = ON as in MSSQL.
Unfortunately, Firebird does not allow you to conditionally drop or create an index in its SQL dialect. If you are executing per statement, you could catch errors and ignore the relevant error codes. Alternatively, you could use an execute block, something like:
execute block as
begin
if (exists(select * from rdb$indices where rdb$index_name = 'IDX_TABLE')) then
execute statement 'drop index IDX_TABLE';
end
The use of execute statement is necessary, because PSQL (the Firebird procedural language) does not support DDL statements directly.
If instead you want to conditionally create an index, you can use:
execute block as
begin
if (not exists(select * from rdb$indices where rdb$index_name = 'IDX_TABLE')) then
execute statement 'create index IDX_TABLE on table (id, name)';
end
The RDB$INDICES table is a Firebird system table.

Firebird with .net driver - drop table if exists

I'm new to Firebird and I'm testing a few things to check out the differences between Fb and SQlite (and the .net driver).
I am trying to do a drop table if exists followed by the creation of a table. In Sqlite I am able to do this by:
command.CommandText = #"DROP TABLE IF EXISTS Persons; CREATE TABLE Persons (
PersonID int,
LastName text,
FirstName text,
Address text,
City text); ";
command.ExecuteNonQuery();
However in Firebird the same query fails. I've read that this is not possible to use IFs directly in Firebird SQL, so I've tried to use:
command.CommandText = #"
EXECUTE BLOCK AS
BEGIN IF EXISTS
(SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$RELATION_NAME = 'Persons')
THEN DROP TABLE Persons; END CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); ";
command.ExecuteNonQuery();
But it fails also with the following error:
Dynamic SQL Error SQL error code = -104 Token unknown - line 1, column
27
Can you please help me on this? I've tried to find more info on the web that could help me, but did not have any luck.
Firebird's SQL syntax doesn't have a drop table if exists, instead use recreate table. recreate table will try to drop the table if it exists before creating it. The syntax of recreate table is - other than recreate instead of create - the same as create table.
Your attempt to use execute block fails for two reasons:
You cannot execute two statements together as a command. If you want to execute a script of multiple statements, you'll need to execute each statement individually or use the FbScript class which will parse the script and execute the individual statements for you.
Even if you execute these statements individually, it will still fail, because PSQL (the stored procedure language used in execute block) does not allow execution of DDL. You can use execute statement to circumvent this limitation, but it is better not to do that. In that way you could also address the previous point by executing both - using execute statement - within the execute block.
Alternatively you could just drop the table unconditionally and catch (and ignore) the resulting exception.

writing a EXECUTE statement in tOracleRow component using context variable

I am trying to write an EXECUTE statement in toraclerow.
At each iteration the string is prepared dynamically from the flow. Here I am not discussing how I have prepared the string. But once prepared the entire string is stored in a single context variable. For example I have the following string stored in a context variable at a given iteration number.
context.FinalString = "Insert into TargetTableName (columnA, columnB)
SELECT Col_A, Col_B
FROM SourceTableName"
I am trying to execute this string in the tOracleRow component using the following statement:
"EXEC SQL EXECUTE '"+context.FinalString+"'"
On running the job I am getting the following error.
ORA-00900: invalid SQL statement
Kindly suggest a solution. Is there a way to execute a sql statement stored as a string in a context variable?
try to execute by removing text EXEC SQL EXECUTE ..just simply the text should be the statement - insert into table select col from table...
or
update table set column=value where ...
you dont need exec sql execute text here.

'Execute Immediate' with Into Clause in HANA

I have a requirement where-in I need to read a table (table name provided as input parameter of the SP), store the results in a temp table and then store the count of the read table into a variable. Please advise how can this be achieved. I have been able to read the table and its count using dynamic query but am not able to put the results in a temp table/ variable. 'Select' and 'Into' clauses do not seem to be working with 'Execute Immediate'. Thanks.
It is not very clear to me exactly what is being asked, but you should be able to execute a SELECT statement in the following manner:
CREATE PROCEDURE p1(IN tablename VARCHAR) AS
BEGIN
execute immediate 'SELECT * FROM ' || :tablename;
END;
Then the following statements create a table and call the procedure to retrieve the result:
create table T (i integer);
insert into T values (123);
The following would produce a result set with one row/column with the value 123:
CALL p1('T')
Please note that with this type of functionality, you need to be very careful not to allow any user-provided input to be given directly to a procedure that uses EXECUTE IMMEDIATE to avoid the possibility of SQL injection attacks.

Insert query in SQL Function

Can i write a insert query inside Function in SQL server 2008. If i tried, im a getting an error of Invalid use of side effecting operator 'INSERT' within the function. Please help me out. But i want it to be a function, not a stored procedure
Create function EFT_DL_FUNC_AUDTI_BATCH_START (#i_db_name varchar(20))
returns int as
begin
insert into table_name(db_name) values (#i_db_name)
return 0
end
Quote from here:
User Defined Functions cannot be used
to modify base table information. The
DML statements INSERT, UPDATE, and
DELETE cannot be used on base tables.
So you can't do an INSERT in a function.
You might want to explain WHY you don't want to use a procedure.