Monday, October 13, 2008

To Conditionally Call Code in SQL*Plus


Since SQL*Plus has no 'If' options (outside of queries) you need a different way to conditionally call another query. One way to do that is to have a query that checks for the condition you're looking for. If it is found, &Ex_If will be set to the name of the query I wish to run. It is first undefined so we're ready for the next execution.

I use this when there are some statements I need to jump around based on conditions. In SQL*Plus, you'll need to move those statements to another query, and then insert this type of code.

My main code is Ex_If.Sql:
Undefine Ex_If                        
Column Ex_If New_Value Ex_If

Select 'RunThis' As Ex_If
From Emp
Where Empno = &Empno;

@&Ex_If

If the condition is found, it calls RunThis.Sql
Select 'This Works!' From Dual;

No comments:

Post a Comment