0

I need to write an oracle sql script based on some condition.

Like User has to type 'y' or 'n' if he types 'y' then i need to

accept gen prompt 'Enter y if you want to generate spool' default 'n'
spool c:\a.spl

    SELECT CUST_ID INTO NO 
       FROM ACC_MASTER 
       WHERE ACCOUNT_NO='&CRN' 
          AND BRANCH_NO='&BRN';


    select * from a where lid=no;
    select * from b where lid=no;
    select * from c where lid=no;

spool off;

If the user types 'N' Nothing needs to be selected.

Please advise

edi9999
  • 19,701
  • 13
  • 88
  • 127

2 Answers2

0

If I were you I would use a bash statement ("if-fi") if you use Linux, or statement "if" for Windows to work around it.

neshkeev
  • 6,280
  • 3
  • 26
  • 47
  • Could you please tell me how do you use if statement in SQL script.Because if i have write like this if (gen=='y') then do all the ops; end if; will not work beacuse selct col into i have to use – user3737084 Jun 26 '14 at 07:56
  • for example for linux for bash you would write `#!/bin/bash echo "Enter y if you want to generate spool, followed by [ENTER]:" read choice if [ "$choice" == "y" ]; then sqlplus / spool c:\a.spl ... spool off; fi` – neshkeev Jun 26 '14 at 08:15
  • I m running through sql developer in windows not unix/linux – user3737084 Jun 26 '14 at 10:27
  • Describe your scenario, I can't understand why you need this. I use the clause "spool" to make sure that my script has been run successfully on other servers by someone (not me). – neshkeev Jun 26 '14 at 11:46
0

Create a noop file first

C:\>echo. > null.sql 

script

set ver off

accept gen prompt 'Enter y if you want to generate spool ' default 'n'

set term off
column col noprint new_value scriptname
select decode(lower('&gen'),'y','myspoolscript','n', 'null') col from dual;
set term on

set ver on

@&scriptname
Bjarte Brandt
  • 4,191
  • 2
  • 23
  • 25