Search code examples
sqloracle-databasesqlplus

Execute two different queries based on date from an sql file


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


Solution

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