PL/SQL - Using IF statements Please use speaker notes for additional information!

Slides:



Advertisements
Similar presentations
Creating an Oracle Database Table Additional information is available in speaker notes!
Advertisements

PL/SQL User Defined Types Record and Table Please use speaker notes for additional information!
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Reports Using SQL Script Please check speaker notes for additional information!
Line Efficiency     Percentage Month Today’s Date
Group functions using SQL Additional information in speaker notes!
Using input variables Please use speaker notes for additional information!
More on IF statements Use speaker notes for additional information!
Unit Number Oct 2011 Nov 2011 Dec 2011 Jan 2012 Feb 2012 Mar 2012 Apr 2012 May 2012 Jun 2012 Jul 2012 Aug 2012 Sep (3/4 Unit) 7 8 Units.
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!
More on variables with Oracle’s SQL*Plus As always, speaker notes will contain additional information!
Totals on the Screen Please use speaker notes for additional information!
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!
Neal Stublen Computer Memory (Simplified)  Remember, all programming decisions came down to a true or false evaluation  Consider.
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!
Exceptions in PL/SQL Please use speaker notes for additional information!
Indexes in Oracle An Introduction Please check speaker notes for additional information!
Introduction to Oracle - SQL Additional information is available in speaker notes!
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
File Handling in QBASIC
Oracle PL/SQL Loops Please use speaker notes for additional information!
Introduction to PL/SQL As usual, use speaker notes for additional information!
More SQL functions As usual,there is additional information in the speaker notes!
Jan 2016 Solar Lunar Data.

Introduction to Triggers
More on Procedures (Internal/Local procedures)
Handling Exceptions.
Introduction to Procedures
Q1 Jan Feb Mar ENTER TEXT HERE Notes

Average Monthly Temperature and Rainfall
Introduction to Functions
Please use speaker notes for additional information!



2017 Jan Sun Mon Tue Wed Thu Fri Sat
Introduction to Views and Reports

Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE

Jan Sun Mon Tue Wed Thu Fri Sat


Electricity Cost and Use – FY 2016 and FY 2017
More and Still More on Procedures and Functions
Relational Operators.
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
Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE

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
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun

Q1 Q2 Q3 Q4 PRODUCT ROADMAP TITLE Roadmap Tagline MILESTONE MILESTONE
Presentation transcript:

PL/SQL - Using IF statements Please use speaker notes for additional information!

PL/SQL - IF SQL> CREATE TABLE first_pay_new 2 AS 3 SELECT * FROM first_pay; Table created. SQL> SELECT * FROM first_pay_new; 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 rows selected. Step 1 for me was to create a new table to use since I will be doing some updating in these examples.

PL/SQL - IF SQL> edit firstpay1 SET VERIFY OFF DECLARE v_pay_id first_pay_new.pay_id%TYPE :=&input_pay_id; v_new_sal first_pay_new.salary%TYPE; v_jobcode first_pay_new.jobcode%TYPE; BEGIN SELECT salary, jobcode INTO v_new_sal, v_jobcode FROM first_pay_new WHERE pay_id = v_pay_id; IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; END IF; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END; / SET VERIFY ON Declare the variables and assign &input_pay_id to v_pay_id so that it will require user input. SELECT one record for a given person and store the jobcode and salary into the specified variables. IF the jobcode on the selected record = CI then the salary stored in the variable is increased by 5% and stored back in the variable using the assignment sign. NOTE the syntax of IF condition THEN processing; END IF; The update changes the salary by making it equal to the variable for the specified record.

PL/SQL - IF SQL> SELECT * FROM first_pay_new; 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 Enter value for input_pay_id: 5555 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 This is the output from the previous slide. The user enters 5555 as the pay_id and since Richard Jones has a jobcode of CI, he receives a 5% raise.

SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF firstpay1 Enter value for input_pay_id: 3333 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 The record 3333 does not have a jobcode of CI so no change was made to the salary.

PL/SQL - IF SQL> edit firstpay1a SET VERIFY OFF DECLARE v_pay_id first_pay_new.pay_id%TYPE :=&input_pay_id; v_new_sal first_pay_new.salary%TYPE; v_jobcode first_pay_new.jobcode%TYPE; BEGIN SELECT salary, jobcode INTO v_new_sal, v_jobcode FROM first_pay_new WHERE pay_id = v_pay_id; IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END IF; END; / SET VERIFY ON The difference in this code is that the UPDATE is now processing within the IF. The update will only be done IF the jobcode that came in with the record was CI.

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 PL/SQL - IF firstpay1a Enter value for input_pay_id: 1111 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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

PL/SQL - IF IF v_jobcode = ‘CI’ Y Calculate new salary Update record on table N IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END IF;

PL/SQL - IF SET VERIFY OFF DECLARE v_pay_id first_pay_new.pay_id%TYPE :=&input_pay_id; v_new_sal first_pay_new.salary%TYPE; v_jobcode first_pay_new.jobcode%TYPE; BEGIN SELECT salary, jobcode INTO v_new_sal, v_jobcode FROM first_pay_new WHERE pay_id = v_pay_id; IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; ELSE v_new_sal := v_new_sal * 1.02; END IF; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END; / SET VERIFY ON SQL> edit firstpay2 If the jobcode stored in the variable is CI then the salary will be increased by 5% otherwise the salary will be increased by 2%

firstpay2 Enter value for input_pay_id: 2222 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 rows selected. SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF Since jobcode is not CI, the salary is increased by 2%.

PL/SQL - IF IF v_jobcode = ‘CI’ Y Calculate new salary with 5% increase N Calculate new salary with 2% increase IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; ELSE v_new_sal := v_new_sal * 1.02; END IF;

PL/SQL - IF SET VERIFY OFF DECLARE v_pay_id first_pay_new.pay_id%TYPE :=&input_pay_id; v_new_sal first_pay_new.salary%TYPE; v_jobcode first_pay_new.jobcode%TYPE; BEGIN SELECT salary, jobcode INTO v_new_sal, v_jobcode FROM first_pay_new WHERE pay_id = v_pay_id; IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; ELSE IF v_jobcode = 'IN' THEN v_new_sal := v_new_sal * 1.03; ELSE v_new_sal := v_new_sal * 1.01; END IF; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END; / SET VERIFY ON SQL> edit firstpay3 The first condition is testing to see if the v_jobcode = CI. If it does, then a 5% increase is calculated. If it is not true, then the false processing after the ELSE asks another question (an inner if). If the inner if condition which is if v_jobcode = IN is true then 1 3% increase is calculated. If the inner if is false then the ELSE condition is executed which calculates a 1% increase. Note that each IF has an END IF.

SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF firstpay3 Enter value for input_pay_id: 8888 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 Since the jobcode is IN, the salary is increased by 3%.

SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF firstpay3 Enter value for input_pay_id: 4444 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 rows selected. The conditions are both false so the else to the second/inner if is executed and salary is increased by 1%.

IF v_jobcode = ‘CI’ Y Calculate new salary with 5% increase N IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; ELSE IF v_jobcode = 'IN' THEN v_new_sal := v_new_sal * 1.03; ELSE v_new_sal := v_new_sal * 1.01; END IF; Calculate new salary with 3% increase IF v_jobcode = ‘IN’ Y N Calculate new salary with 3% increase PL/SQL - IF

SET VERIFY OFF DECLARE v_pay_id first_pay_new.pay_id%TYPE :=&input_pay_id; v_new_sal first_pay_new.salary%TYPE; v_jobcode first_pay_new.jobcode%TYPE; BEGIN SELECT salary, jobcode INTO v_new_sal, v_jobcode FROM first_pay_new WHERE pay_id = v_pay_id; IF v_jobcode = 'CI' THEN v_new_sal := v_new_sal * 1.05; ELSIF v_jobcode = 'IN' THEN v_new_sal := v_new_sal * 1.03; ELSE v_new_sal := v_new_sal * 1.01; END IF; UPDATE first_pay_new SET salary = v_new_sal WHERE pay_id = v_pay_id; END; / SET VERIFY ON SQL> edit firstpay4 PL/SQL - IF

SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF firstpay4 Enter value for input_pay_id: 3333 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 Processing went to the ELSE where a 1% increase was given.

SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF firstpay4 Enter value for input_pay_id: 6666 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 Record 6666 has a jobcode of IN so the answer to the ELSIF was true and the salary was increased by 3%.