More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Variables SQL> SELECT * 2 FROM first_pay 3 ORDER BY &ordcol1, &ordcol2; Enter value for ordcol1: jobcode Enter value for ordcol2: bonus old 3: ORDER BY &ordcol1, &ordcol2 new 3: ORDER BY jobcode, bonus PAY_ NAME JO STARTDATE SALARY BONUS Susan Ash AP 05-FEB Linda Costa CI 15-JAN Richard Jones CI 30-OCT Donald Brown CI 05-NOV Stephen York CM 03-JUL John Davidson IN 25-SEP Joanne Brown IN 18-AUG Paula Adams IN 12-DEC In this example, the user will enter the field for the primary sort and the field for the secondary sort.
SQL> SELECT &col, COUNT(&col) 2 FROM first_pay 3 GROUP BY &col; Enter value for col: jobcode old 1: SELECT &col, COUNT(&col) new 1: SELECT jobcode, COUNT(jobcode) Enter value for col: jobcode old 3: GROUP BY &col new 3: GROUP BY jobcode JO COUNT(JOBCODE) AP 1 CI 3 CM 1 IN 3 SQL> SELECT &&col, COUNT(&col) 2 FROM first_pay 3 GROUP BY &col; Enter value for col: bonus old 1: SELECT &&col, COUNT(&col) new 1: SELECT bonus, COUNT(bonus) old 3: GROUP BY &col new 3: GROUP BY bonus BONUS COUNT(BONUS) Variables In the example to the left, I used &col. Notice that the user had to enter jobcode three separate times. In the example below, I used &&col at the beginning of the select. Notice that the user only had to enter the value once. The stored variable was used from that point on.
Predefine variables - define SQL> DEFINE coldef = jobcode; SQL> SELECT name, salary, &coldef 2 FROM first_pay; old 1: SELECT name, salary, &coldef new 1: SELECT name, salary, jobcode NAME SALARY JO Linda Costa CI John Davidson IN Susan Ash AP Stephen York CM Richard Jones CI Joanne Brown IN Donald Brown CI Paula Adams IN In this example, I used DEFINE to predefine the field coldef. I then went on and used &coldef in a SELECT statement and no user entry was required.
SQL> SELECT name, &&colin 2 FROM first_pay; Enter value for colin: salary old 1: SELECT name, &&colin new 1: SELECT name, salary NAME SALARY Linda Costa John Davidson Susan Ash Stephen York Richard Jones Joanne Brown Donald Brown Paula Adams rows selected. SQL> DEFINE colin; DEFINE COLIN = "salary" (CHAR) Variables The variable colin was defined by using the &&colin in a SELECT. The user was then prompted to enter the value for colin. To check the definition, I can simply enter DEFINE followed by the name of the variable. It will return the contents and the type which is CHAR
SQL> DEFINE colin; DEFINE COLIN = "salary" (CHAR) SQL> DEFINE jobci = CI; SQL> DEFINE jobci; DEFINE JOBCI = "CI" (CHAR) SQL> SELECT * 2 FROM first_pay 3 WHERE jobcode = '&jobci'; old 3: WHERE jobcode = '&jobci' 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 Define This is the definition of colin from the previous slide. Now I am setting up a definition of jobci and giving it the value of CI. When I look at this definition, it returns jobci = CI in a CHAR field. When I use &jobci in a SELECT there is no prompt for user entry since jobci has been previously defined.
Accept SQL> DEFINE jobci; DEFINE JOBCI = "CI" (CHAR) SQL> ACCEPT jobci; IN SQL> DEFINE jobci; DEFINE JOBCI = "IN" (CHAR) SQL> ACCEPT jobci PROMPT 'Please enter the new jobcode you are processing: ' Please enter the new jobcode you are processing: CM SQL> Define jobci; DEFINE JOBCI = "CM" (CHAR) First I checked to see what value was stored in jobci. I did this by entering DEFINE jobci. It returned the definition as CI (CHAR). Next I keyed in ACCEPT jobci which will allow me to enter in a new value to be stored in the variable jobci. The cursor moved down a line and waited. I entered IN. Then I wanted to see if the change had happened. I keyed in DEFINE jobci; and it returned the definition which now contained IN. When I did ACCEPT the first time, it simply waited for me to key in something else. I decided I wanted a prompt to tell me what to enter and to tell me it was time to enter data. I entered the prompt and when it executed it came up with the prompt. I responded with CM. I then did a DEFINE jobci to make sure the CM was there. It was.
SQL> ACCEPT v_bonus NUMBER PROMPT 'Please enter bonus to check against: ' Please enter bonus to check against: 2000 Accept SQL> DEFINE v_bonus; DEFINE V_BONUS = 2000 (NUMBER) SQL> SELECT * 2 FROM first_pay 3 WHERE bonus = &v_bonus; old 3: WHERE bonus = &v_bonus new 3: WHERE bonus = 2000 PAY_ NAME JO STARTDATE SALARY BONUS Stephen York CM 03-JUL Richard Jones CI 30-OCT Joanne Brown IN 18-AUG Paula Adams IN 12-DEC First I used the accept to setup a variable called v_bonus as a NUMBER with a PROMPT. The next line shows the prompt. I entered To check, I used the DEFINE v_bonus. The variable v_bonus has been given the value 2000 and defined as a NUMBER. I then used the variable &v_bonus in the SELECT. Since it was already defined, it does not require user input.
Accept SQL> ACCEPT v_price NUMBER FORMAT PROMPT 'Enter price to compare against: ' Enter price to compare against: SQL> SELECT * 2 FROM inven 3 WHERE price >= &v_price; old 3: WHERE price >= &v_price new 3: WHERE price >= ITEM ITEMNAME ONHAND ONORDER REORDPT COST PRICE DE IT LOCA Heidi BK CH X Teddy Bear TY CH X Building Blocks TY CH Z Doll House TY CH Z Basketball SP BK Y Net/Hoop SP BK Y200 SQL> SELECT * FROM inven; ITEM ITEMNAME ONHAND ONORDER REORDPT COST PRICE DE IT LOCA Good Night Moon BK BY X Heidi BK CH X Adven Reddy Fox BK CH X Teddy Bear TY CH X Building Blocks TY CH Z Doll House TY CH Z Basketball SP BK Y Net/Hoop SP BK Y200 First I defined v_price as a number, gave it a format and include a prompt. When the prompt came up, I entered This assigns this amount to v_price. Then I did a select for all records where the price was greater than or equal to &v_price.
Set verify off SQL> SET VERIFY OFF SQL> SELECT * 2 FROM inven 3 WHERE price >= &v_price; ITEM ITEMNAME ONHAND ONORDER REORDPT COST PRICE DE IT LOCA Heidi BK CH X Teddy Bear TY CH X Building Blocks TY CH Z Doll House TY CH Z Basketball SP BK Y Net/Hoop SP BK Y200 SQL> SET VERIFY ON SQL> SELECT * 2 FROM inven 3 WHERE price >= &v_price; old 3: WHERE price >= &v_price new 3: WHERE price >= ITEM ITEMNAME ONHAND ONORDER REORDPT COST PRICE DE IT LOCA Heidi BK CH X Teddy Bear TY CH X Building Blocks TY CH Z Doll House TY CH Z Basketball SP BK Y Net/Hoop SP BK Y200 SET VERIFY OFF suppresses the display of old and new. SET VERIFY ON continues the display of old and new.