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

Using input variables Please use speaker notes for additional information!

Embed Size (px)

Citation preview

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

Using input variables

Please use speaker notes for additional information!

Page 2: Using input variables Please use speaker notes for additional information!

User input variablesUser input variables

SQL> SELECT * 2 FROM first_pay 3 WHERE jobcode = &jobcode;Enter value for jobcode: 'CI'old 3: WHERE jobcode = &jobcodenew 3: WHERE jobcode = 'CI'

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10005555 Richard Jones CI 30-OCT-92 50000 20007777 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 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 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.

Page 3: Using input variables Please use speaker notes for additional information!

SQL> SELECT * FROM first_pay WHERE jobcode = '&jobcode';Enter value for jobcode: CIold 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 10005555 Richard Jones CI 30-OCT-92 50000 20007777 Donald Brown CI 05-NOV-99 45000

User input variablesUser input variables SQL> SELECT * 2 FROM first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 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.

Page 4: Using input variables Please use speaker notes for additional information!

Using input variablesUsing input variables SQL> SELECT * 2 FROM first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 Paula Adams IN 12-DEC-98 45000 2000

SQL> SELECT * 2 FROM first_pay 3 WHERE salary = &salary;Enter value for salary: 50000old 3: WHERE salary = &salarynew 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.

Page 5: Using input variables Please use speaker notes for additional information!

Using input variablesUsing input variables SQL> SELECT * 2 FROM first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 450008888 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 yorkold 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.

Page 6: Using input variables Please use speaker notes for additional information!

User input variablesUser input variables

old 3: WHERE &condnamenew 3: WHERE jobcode = 'CI'

NAME SALARY JO-------------------- --------- --Linda Costa 45000 CIRichard Jones 50000 CIDonald Brown 45000 CI

SQL> SELECT &col1_tosee, &col2_tosee, &col3_tosee 2 FROM &tablename 3 WHERE &condname;Enter value for col1_tosee: nameEnter value for col2_tosee: salaryEnter value for col3_tosee: jobcodeold 1: SELECT &col1_tosee, &col2_tosee, &col3_toseenew 1: SELECT name, salary, jobcodeEnter value for tablename: first_payold 2: FROM &tablenamenew 2: FROM first_payEnter 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.

Page 7: Using input variables Please use speaker notes for additional information!

Using input variablesUsing input variables

SQL> SELECT &selectinfo;Enter value for selectinfo: name, salary, jobcode FROM first_pay WHERE jobcode = 'CI'old 1: SELECT &selectinfonew 1: SELECT name, salary, jobcode FROM first_pay WHERE jobcode = 'CI'

NAME SALARY JO-------------------- --------- --Linda Costa 45000 CIRichard Jones 50000 CIDonald Brown 45000 CI

Page 8: Using input variables Please use speaker notes for additional information!

Using input variablesUsing input variablesSQL> SELECT name, salary, &&jobentry 2 FROM first_pay 3 WHERE &&jobentry = 'CI';Enter value for jobentry: jobcodeold 1: SELECT name, salary, &&jobentrynew 1: SELECT name, salary, jobcodeold 3: WHERE &&jobentry = 'CI'new 3: WHERE jobcode = 'CI'

NAME SALARY JO-------------------- --------- --Linda Costa 45000 CIRichard Jones 50000 CIDonald Brown 45000 CI

SQL> SELECT name, salary, &&jobentry 2 FROM first_pay 3 WHERE &&jobentry = 'CI';old 1: SELECT name, salary, &&jobentrynew 1: SELECT name, salary, jobcodeold 3: WHERE &&jobentry = 'CI'new 3: WHERE jobcode = 'CI'

NAME SALARY JO-------------------- --------- --Linda Costa 45000 CIRichard Jones 50000 CIDonald 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.