2

I am making this program that will have a menu that gets the user's input and performs a certain script based on his/her choice. Something along the lines of:

Please make a selection:
1: Do script a
2: Do script b
3: Do script c

I looked at this link : How to make SQLPlus or PL/SQL Menu

And I was able to get something out of it, BUT it's not fully functional. Because there are certain lines I don't understand. See below.

PROMPT  1: Make a sales invoice 
PROMPT  2: Inquire a sales invoice

accept selection PROMPT "Enter option 1-2: "
set term off
column script new_value v_script  -- What's column script?
select case '&selection.'
       when '1' then '@test.sql'
       when '2' then '@test.sql'
       else '@FinalAssignment.sql'
       end as script  -- What script is this referring to? 
from dual; -- Don't know this

set term on

@&v_script. -- What script is being ran here?

I can get the program to run the scripts, BUT the else doesn't really work. If I pick a number outside that range the script will close, instead of reloading itself. Also when a script is ran it closes itself, instead of going back to the main menu. I know I have to run a loop to fix this problem, BUT I don't know how to implement it in this language it.

Any help is appreciated it. Thanks!

Community
  • 1
  • 1

1 Answers1

2

Ans 1. Column script is the column in the select query defined by as script, which means script is a column alias.

Ans 2. as script is referring to as a column alias of the select stmt. There is only one column in this select statement. Example SELECT column AS col1 FROM table;. The column NEW_VALUE gets the selected value (which is driven from the case stmt, which in this case is the script name test1.sql, test2.sql, or FinalAssignment.sql) and stores it into v_script.

Ans 3. dual table is a special one-row table present by default in all Oracle database installations. More on dual here. You can select anything from dual, like select sysdate from dual; or select 'ABCDEF' AS col1 from dual;.

Ans 4. The v_script column will contain the script name as per your selection from the case statement in the select query, i.e. from the menu selection (as discussed in Ans 2.). Once that is selected, you may want to run that selected script (Wouldn't you?). Thats what @&v_script does. You run a script in SQLPlus using @script_name

PROMPT  1: Make a sales invoice 
PROMPT  2: Inquire a sales invoice    
accept selection PROMPT "Enter option 1-2: "

set term off
column script new_value v_script  --Q1. What's column script?
select case '&selection.'          --from accept above
       when '1' then '@test1.sql'  --script to run when chosen option 1.
       when '2' then '@test2.sql'  --script to run when chosen option 2.
       else '@FinalAssignment.sql' --this script
       end as script  --Q2. What script is this referring to? 
from dual; --Q3. Don't know this

set term on

@&v_script. --Q4. What script is being ran here?

Caveats-

  1. FinalAssignment.sql should be the name of the script itself i.e. the script where the above code is.

  2. Line 1,2, and 3 are part of the script. PROMPT is a valid SQL*Plus command and so is ACCEPT.

Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
Anjan Biswas
  • 7,746
  • 5
  • 47
  • 77