Using input variables Please use speaker notes for additional information!
User input variables SQL> SELECT * 2 FROM first_pay 3 WHERE jobcode = &jobcode; Enter value for jobcode: 'CI' old 3: WHERE jobcode = &jobcode new 3: WHERE jobcode = 'CI' PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN Richard Jones CI 30-OCT Donald Brown CI 05-NOV SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC An & in front a an undefined variable name will cause a prompt to the user asking for the contents of the variable. It will then respond with the old which is the variable name with the & in front and the new which is the value that was entered. Note that when I keyed in the jobcode of CI, I surrounded in with single quotes because it was a string variable. All the records with jobcode of CI are displayed.
SQL> SELECT * FROM first_pay WHERE jobcode = '&jobcode'; Enter value for jobcode: CI old 1: SELECT * FROM first_pay WHERE jobcode = '&jobcode' new 1: SELECT * FROM first_pay WHERE jobcode = 'CI' PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN Richard Jones CI 30-OCT Donald Brown CI 05-NOV User input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC In this example, the whole select is on one line so when the old and new come back the entire select is shown, not just the part that contains the prompt. NOTE: In this example the &jobcode is enclosed in single quotes within the select. Therefore, when the user enters the jobcode of CI, it does not have to be enclosed in quotes. Notice that when the new is shown, CI is enclosed in the necessary single quotes.
Using input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC SQL> SELECT * 2 FROM first_pay 3 WHERE salary = &salary; Enter value for salary: old 3: WHERE salary = &salary new 3: WHERE salary = PAY_ NAME JO STARTDATE SALARY BONUS Richard Jones CI 30-OCT Salary is numeric so single quotes are not needed as shown in this example.
Using input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS Linda Costa CI 15-JAN John Davidson IN 25-SEP Susan Ash AP 05-FEB Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Donald Brown CI 05-NOV Paula Adams IN 12-DEC SQL> SELECT * 2 FROM first_pay 3 WHERE UPPER(name) = UPPER('&name'); Enter value for name: stephen york old 3: WHERE UPPER(name) = UPPER('&name') new 3: WHERE UPPER(name) = UPPER('stephen york') PAY_ NAME JO STARTDATE SALARY BONUS Stephen York CM 03-JUL In this example, the user enters the name without quotes using any combination of upper and lower case. The name will be converted to upper for the comparison. Notice the name is also enclosed in single quotes when the new is shown. The name on the table is also converted to upper case so that we will match upper case to upper case.
User input variables old 3: WHERE &condname new 3: WHERE jobcode = 'CI' NAME SALARY JO Linda Costa CI Richard Jones CI Donald Brown CI SQL> SELECT &col1_tosee, &col2_tosee, &col3_tosee 2 FROM &tablename 3 WHERE &condname; Enter value for col1_tosee: name Enter value for col2_tosee: salary Enter value for col3_tosee: jobcode old 1: SELECT &col1_tosee, &col2_tosee, &col3_tosee new 1: SELECT name, salary, jobcode Enter value for tablename: first_pay old 2: FROM &tablename new 2: FROM first_pay Enter value for condname: jobcode = 'CI' In this example, SELECT, FROM and WHERE are hard coded. All the other information comes from user input at the variable prompt.
Using input variables SQL> SELECT &selectinfo; Enter value for selectinfo: name, salary, jobcode FROM first_pay WHERE jobcode = 'CI' old 1: SELECT &selectinfo new 1: SELECT name, salary, jobcode FROM first_pay WHERE jobcode = 'CI' NAME SALARY JO Linda Costa CI Richard Jones CI Donald Brown CI
Using input variables SQL> SELECT name, salary, &&jobentry 2 FROM first_pay 3 WHERE &&jobentry = 'CI'; Enter value for jobentry: jobcode old 1: SELECT name, salary, &&jobentry new 1: SELECT name, salary, jobcode old 3: WHERE &&jobentry = 'CI' new 3: WHERE jobcode = 'CI' NAME SALARY JO Linda Costa CI Richard Jones CI Donald Brown CI SQL> SELECT name, salary, &&jobentry 2 FROM first_pay 3 WHERE &&jobentry = 'CI'; old 1: SELECT name, salary, &&jobentry new 1: SELECT name, salary, jobcode old 3: WHERE &&jobentry = 'CI' new 3: WHERE jobcode = 'CI' NAME SALARY JO Linda Costa CI Richard Jones CI Donald Brown CI SQL> UNDEFINE jobentry; The use of && means that the user does not have to reenter the information. It is stored in a reusable variable. Note that the first time this SELECT was executed the prompt Enter value for jobentry came up and the user entered jobcode. Note that the second time the SELECT was execute the prompt did not appear and the SELECT got executed automatically using the reusable variable. To clear a reusable variable, use UNDEFINE followed by the name of the reusable variable.