writing a EXECUTE statement in tOracleRow component using context variable - sql

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.

Related

SQL - stored procedure, pass field and table name parameters on execute

I created a SQL procedure that replaces all values in a field with Xs the same length as the original values.
Update Table
Set Name = Replicate('x', Len(Name))
I am trying to alter this procedure so that I can just pass a table and field name as a parameter on execute instead of editing the stored procedure every time I want to pass a new field. Is there a way to do this?
This is what I think the execute statements should look like when I want to x out the values in another field:
Exec MyProcedure ‘Users’, ‘Email’
You can use EXEC to execute sql statements like this: EXEC (#sqlCommand). SqlCommand would be the string composed by you based on the received parameters.
Also, another option would be to run the statement using sp_executesql.

Executing DDL in compound SQL using DashDB (DB2)

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...'

Error Insert Into a table variable Hana

I'm working passing ms sql server statements into hana sql statements. I have a variable (Tab) type Table and a variable string (query) defined as:
Hana Statement
CREATE TYPE "tab_table_TYPE" AS TABLE ("DocEntry" integer, "LineId" integer, "VisOrder" integer, "Object" nvarchar(20));
v_Tab tab_table_TYPE
query := 'SELECT [DocEntry],[LineId],[VisOrder] ,[Object] FROM [#INV_AFC]';
so I'm trying to convert this Ms Sql Statement into a Hana Statement :
Ms Sql Server Statement
INSERT INTO #v_Tab([DocEntry],[LineId],[VisOrder],[Object]) exec (#query)
I wish to use an internal table type variable which can hold the resultset from the query!
When I use the Sql Converter with this sentence displays this error:
--[Note:ErrorModifier] INSERT INTO statement does not support EXEC; use EXEC('insert into table '+ originalstatement)
--[Note:Stringifier] SAP HANA does not support InsertTarget
Finally the question is : How would be the correct Hana Sql Statement for this case?
The syntax of your table-type creation is correct. I guess you are trying to execute the query inside a procedure.
Inside a procedure, you have different options. If your query is only a partial result and you want to run further statements on the result set of the query, you don't need to initialize a table variable at all. Just assign a variable to a resultset:
table_variable = SELECT DocEntry, LineId, VisOrder, Object FROM INV_AFC;
// Use table_variable for further statements, for example:
SELECT count(*) INTO resultSetCount FROM :table_variable;
If your query is already the final result, you can easily define an output variable and directly assign your result set as output variable. For example:
CREATE PROCEDURE "YOURSCHEMA"."SomeProcedureName" (
in someInputVariable1 NVARCHAR(255),
in someInputVariable2 BIGINT,
out tableVariable "YOURSCHEMA".tab_table_TYPE)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER AS
BEGIN
tableVariable = SELECT DocEntry, LineId, VisOrder, Object FROM INV_AFC;
END;
When you then call this procedure the 3rd parameter will automatically contain your result set:
call "YOURSCHEMA"."SomeProcedureName"('someString', 123, ?);

'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.

MySQL - Using stored procedure results to define an IN statement

I'd like to use a stored procedure to define the IN clause of a select statement.
This is (a simplified version of) what I'm trying to do:
SELECT *
FROM myTable
WHERE columnName IN (CALL myStoredProc)
myStoredProc performs some complicated logic in the database and returns a list of possible matching values for columnName. The statement above does not work obviously. The select statement may be performed in another stored procedure if that makes a difference.
Is this at all possible in mySQL?
What return type does your current stored procedure have? You are speaking of "a list", so TEXT?
Maybe there's an easier way, but one thing you can do (inside another stored procedure) is to build another query.
To do that, we need to work around two limitations of MySQL: a) To execute dynamic SQL inside a stored procedure, it needs to be a prepared statement. b) Prepared statements can only be created out of user variables. So the complete SQL is:
SET #the_list = myStoredProc();
SET #the_query = CONCAT('SELECT * FROM myTable WHERE columnName IN (' , #the_list , ')');
PREPARE the_statement FROM #the_query;
EXECUTE the_statement;
If you're talking about returning a result set from a stored routine and then using it as table, that is not possible. You need to make a temporary table to work around this limitation.