Execute two different queries based on date from an sql file - sql

I want to execute two different queries based on date from an sql file.
Suppose I have two merge queries , Merge query 1, Merge query 2.
Most importantly this has to be within an sql file and run from a script using sqlplus.
Condition is like this,
If current date is "April 1st"
Execute Merge query 1
For all other dates
Execute Merge query 2
I want to avoid stored procedure. This is in Oracle

The sqlplus doesn't provide flow control (like IF statement) so the cleanest option is to use pl/sql block in the SQL script. Not a stored procedure just embed the whole logic into anonymous pl/sql block
DECLARE
...
BEGIN
END;
/
You go with selecting current date into a variable.
SELECT TO_CHAR(sysdate, 'YYYY-MM-DD') INTO some_var FROM DUAL
And execute queries inside of regular IF statement.

Related

Execute stored procedure in a loop for set of records

I want to write a stored procedure to loop through set of records and for each of the record execute another stored procedure.
Select query returning list of id's:
select id
from abc
where some condition
I have a stored procedure usp_get_data #id=id which I want to execute for each of the rows of the select query. I do not want to use cursor. What are the other ways I can achieve this?
foreach(item in select statement)
{
execute stored procedure
}
The sub stored procedure will return a list of records which I will then return back from the main stored procedure
I do not want to use cursor. What are the other ways I can achieve this?
Rewirte the stored procedure to operate over a set of data. You can pass bulk data to a stored procedure using a table-valued parameter, or by loading data into a Temp table, which the stored procedure then uses.
Or use a cursor. They aren't the worst thing, and "calling a stored procedure for each row" is probably the most common legitimate use of a cursor. Also the looping-without-a-cursor solutions are all dumb and worse than using a cursor.

How to get the last executed SQL statement in HANA?

I am inside a SQLScript procedure and would like to return the last SQL Statement from this procedure, like the last rowcount:
/********* Begin Procedure Script ************/
BEGIN
select 1 as "my_data" from dummy;
select '::last_SQL' as "last executed sql" from dummy;
select ::ROWCOUNT as "rowcount" from dummy;
END;
/********* End Procedure Script ************/
The column "last executed SQL" should be populated with select 1 as "my_data" from dummy in this case. Is there any variable holding the last statement (or any easy way to retrieve the query plan)?
Maybe you can query sys.m_sql_plan_cache system view
Please check following SELECT statement
select
statement_string, last_execution_timestamp
from SYS.M_SQL_PLAN_CACHE
where user_name = 'KODYAZ'
order by last_execution_timestamp desc;
I believe you can improve the query by introducing new filter criteria.
There is no way to programmatically get the last executed SQL statement in SQLScript. This is due to the fact that not all statements will be executed in the form and order as they appear in the source code.
If you want to analyse the performance of a procedure, you can run PlanViz against the procedure call. Generally, there is no such thing as "the performance of a procedure/function" as they always occur in a specific context.
If used within a SQL query, things like query-transformation can radically change the way certain results are computed.

PLSQL Execute Immediate with Dynamic Using

I am dynamically building a search query with bind variables with at least 1 and at most 7 different potential criteria. I know I can do this -
EXECUTE IMMEDIATE sql USING bind_var1, bind_var2 or
EXECUTE IMMEDIATE sql USING bind_var3, bind_var5, bind_var7.
Is it possible to include the bind variables within the sql?
sql = 'SELECT * FROM table WHERE id = :bind_var1 AND name = :bind_var2 USING bind_var1, bind_var2'
and do
EXECUTE IMMEDIATE sql?
I want and need to dynamically build the USING piece instead of writing a lot of IF THEN statements.
According to your tags, I assume this will be used inside some kind of PL/SQL block. So, maybe are you looking for the open for statement.
This allows you to get a cursor on an dynamic query:
sql := 'SELECT * FROM table WHERE id = :bind_var1 AND name = :bind_var2';
open my_cursor for sql using bind_var1, bind_var2';
-- do whatever you need with your cursor
your USING bind_var1, bind_var2 pice of code should be out side os your sql string and come at the end of execute immediate statement and also for select senarios try to use dynamic sql for select with a cursor unless you want to select into a variable

Use Dynamic Query in a Procedure and call that procedure in a select statement in SQL Server

I need to execute a dynamic query in SQL Server and show the result in a select query.
Since, we cannot use dynamic queries in functions, we have written a stored procedure to return the dynamic query value.
But how can i call my stored procedure in a select query.
You normally would use the key word EXEC or EXECUTE to call a stored procedure.
yet there is a way of selecting data from a stored procedure using OPENQUERY.
SELECT * FROM
OPENQUERY(SERVERNAME, 'EXECUTE Proc_Name #parameters')

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.