Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using input variables Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Using input variables Please use speaker notes for additional information!"— Presentation transcript:

1 Using input variables Please use speaker notes for additional information!

2 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 5555 Richard Jones CI 30-OCT-92 50000 2000 7777 Donald Brown CI 05-NOV-99 45000 SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 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.

3 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 5555 Richard Jones CI 30-OCT-92 50000 2000 7777 Donald Brown CI 05-NOV-99 45000 User input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 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.

4 Using input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 SQL> SELECT * 2 FROM first_pay 3 WHERE salary = &salary; Enter value for salary: 50000 old 3: WHERE salary = &salary new 3: WHERE salary = 50000 PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 5555 Richard Jones CI 30-OCT-92 50000 2000 Salary is numeric so single quotes are not needed as shown in this example.

5 Using input variables SQL> SELECT * 2 FROM first_pay; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 45000 1000 2222 John Davidson IN 25-SEP-92 40000 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42000 2000 5555 Richard Jones CI 30-OCT-92 50000 2000 6666 Joanne Brown IN 18-AUG-94 48000 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 45000 2000 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 ---- -------------------- -- --------- --------- --------- 4444 Stephen York CM 03-JUL-97 42000 2000 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.

6 User input variables old 3: WHERE &condname new 3: WHERE jobcode = 'CI' NAME SALARY JO -------------------- --------- -- Linda Costa 45000 CI Richard Jones 50000 CI Donald Brown 45000 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.

7 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 45000 CI Richard Jones 50000 CI Donald Brown 45000 CI

8 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 45000 CI Richard Jones 50000 CI Donald Brown 45000 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 45000 CI Richard Jones 50000 CI Donald Brown 45000 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.


Download ppt "Using input variables Please use speaker notes for additional information!"

Similar presentations


Ads by Google