More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!

Slides:



Advertisements
Similar presentations
Introduction to ReportSmith and Effective Dated Tables
Advertisements

Creating an Oracle Database Table Additional information is available in speaker notes!
PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
Reports Using SQL Script Please check speaker notes for additional information!
Order Entry System Please use speaker notes for additional information!
Group functions using SQL Additional information in speaker notes!
Relational example using donor, donation and drive tables For additional information see the speaker notes!
Using input variables Please use speaker notes for additional information!
PL/SQL - Using IF statements Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Table maintenance revisited (again) Please use speaker notes for additional information!
SQL Use of Functions Character functions Please use speaker notes for additional information!
SQL functions - numeric and date Speaker notes contain additional information!
Cursors in PL/SQL Includes cursor example and continuation of first cursor example Please use speaker notes for additional information!
Cursor and Exception Handling By Nidhi Bhatnagar.
Database Applications – Microsoft Access Lesson 9 Designing Special Queries.
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
PHP meets MySQL.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Totals on the Screen Please use speaker notes for additional information!
Dr. Chen, Business Database Systems (Oracle) Instruction on Creating sql files and SPOOL Jason C.H. Chen, Ph.D. Professor of MIS School of Business, Gonzaga.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Cursors These slides are licensed under.
Examples dealing with cursors and tables Please use speaker notes for additional information!
More on views Please refer to speaker notes for additional information!
SQL and Conditions Speaker notes will provide additional information!
Manipulating data within PL/SQL Please use speaker notes for additional information!
Oracle Command Spool Spool C:\temp\Lab9.lst Select Hotel_no, room_no, type, price From Room Order by Hotel_no; Spool Off.
Exceptions in PL/SQL Please use speaker notes for additional information!
Advanced SQL: Triggers & Assertions
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Agenda Basic Logic Purpose if statement if / else statement
Introduction to Oracle - SQL Additional information is available in speaker notes!
1. switch statement 2. “Nested” statements Conditionals, part 2 1.
Maximum Profit Please use speaker notes for additional information!
Introduction to PL/SQL As usual, use speaker notes for additional information!
Loop Assignment There are no speaker notes to accompany this assignment.
More SQL functions As usual,there is additional information in the speaker notes!
Jan 2016 Solar Lunar Data.

More on Procedures (Internal/Local procedures)
Introduction to Procedures
Q1 Jan Feb Mar ENTER TEXT HERE Notes
Access Database for CIS17
MySQL - Creating donorof database offline
Project timeline # 3 Step # 3 is about x, y and z # 2
Average Monthly Temperature and Rainfall
Please use speaker notes for additional information!

Using SQL with Access I am adding queries to the stu table in SecondDB.accdb – a database that I created in class. SQL stands for structured query language.
Introduction to Views and Reports
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Access Database for CIT12
More and Still More on Procedures and Functions
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Project timeline # 3 Step # 3 is about x, y and z # 2
This is an introduction to JavaScript using the examples found at the CIS17 website. In previous examples I specified language = Javascript, instead of.
Loop Assignment.
Presentation transcript:

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.