'Execute Immediate' with Into Clause in HANA - 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.

Related

is it possible to do "CREATE OR REPLACE TABLE foo_{#run_time|"%Y%m%d"} AS" in a scheduled query?

I am wondering if/how i can run a CTAS type statement via a scheduled query but wrangle the #run_date or #run_time param to be the yyyymmdd suffix of the table i want to create or replace.
So the scheduled query would look like:
CREATE OR REPLACE TABLE foo_{#run_date|"%Y%m%d"} AS
select 'hello'
Such that if i was to run it on date of '2021-07-01' i would create the table called foo_20210701
I'm just not quite sure for to wrangle the default #run_date param to include it in my table name.
As mentioned in a answer to a similar question and in the documentation:
Parameters cannot be used as substitutes for identifiers, column names, table names, or other parts of the query.
There is however a workaround. You could use EXECUTE IMMEDIATE to run a SQL script defined as a string. Eg.
EXECUTE IMMEDIATE "SELECT CURRENT_DATE()"
Although it is very hacky it could be used like this achieve what you need.
EXECUTE IMMEDIATE CONCAT('CREATE TABLE `some_project.some_dataset.foo_', CURRENT_DATE(), '` AS SELECT "hello" as column_name' );
And below an approach using a DECLARE statement to keep the table name as a variable
DECLARE table_name STRING DEFAULT CAST(CURRENT_DATE() AS STRING);
EXECUTE IMMEDIATE
CONCAT('CREATE TABLE `some_project.some_dataset.foo_', table_name, '` AS SELECT "hello" as column_name' );

Analyze_Compensation in oracle

Can anyone please tell me for what purpose analyze_compensation is used in oracle?
I got this term in a code in bulk collect. PFB the link:
http://www.oracle.com/partners/campaign/o28plsql-095155.html
Suppose you want to print all the rows and columns of table i.e(Select * from table_name;
) in PL/SQL
THEN in that case :-
dbms_output.put_line() wont be able to print the table data when there is more than one row... Hence we use analyze_compensation() .
It is a procedure , which you need to declare in DECLARE BLOCK of PL/SQL
analyze_compensation is used to print the whole table data i.e Select * from table_name;

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, ?);

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.

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.