Download presentation
Presentation is loading. Please wait.
Published byRobert Franklin Modified over 9 years ago
1
PL/SQL - Using IF statements Please use speaker notes for additional information!
2
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 ---- -------------------- -- --------- --------- --------- 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 8 rows selected. Step 1 for me was to create a new table to use since I will be doing some updating in these examples.
3
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.
4
PL/SQL - IF SQL> SELECT * FROM first_pay_new; 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> @firstpay1 Enter value for input_pay_id: 5555 PL/SQL procedure successfully completed. SQL> SELECT * FROM first_pay_new; 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 52500 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 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.
5
SQL> SELECT * FROM first_pay_new; 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 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 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 The record 3333 does not have a jobcode of CI so no change was made to the salary.
6
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.
7
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 52500 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 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 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 52500 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
8
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;
9
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%
10
SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 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 52500 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 8 rows selected. SQL> SELECT * FROM first_pay_new; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 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 52500 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 PL/SQL - IF Since jobcode is not CI, the salary is increased by 2%.
11
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;
12
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.
13
SQL> SELECT * FROM first_pay_new; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 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 52500 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 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 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 52500 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 46350 2000 Since the jobcode is IN, the salary is increased by 3%.
14
SQL> SELECT * FROM first_pay_new; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 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 52500 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 46350 2000 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42420 2000 5555 Richard Jones CI 30-OCT-92 52500 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 46350 2000 8 rows selected. The conditions are both false so the else to the second/inner if is executed and salary is increased by 1%.
15
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
16
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
17
SQL> SELECT * FROM first_pay_new; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 1500 3333 Susan Ash AP 05-FEB-00 25000 500 4444 Stephen York CM 03-JUL-97 42420 2000 5555 Richard Jones CI 30-OCT-92 52500 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 46350 2000 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 1500 3333 Susan Ash AP 05-FEB-00 25250 500 4444 Stephen York CM 03-JUL-97 42420 2000 5555 Richard Jones CI 30-OCT-92 52500 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 46350 2000 Processing went to the ELSE where a 1% increase was given.
18
SQL> SELECT * FROM first_pay_new; PAY_ NAME JO STARTDATE SALARY BONUS ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 1500 3333 Susan Ash AP 05-FEB-00 25250 500 4444 Stephen York CM 03-JUL-97 42420 2000 5555 Richard Jones CI 30-OCT-92 52500 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 46350 2000 PL/SQL - IF SQL> @ 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 ---- -------------------- -- --------- --------- --------- 1111 Linda Costa CI 15-JAN-97 47250 1000 2222 John Davidson IN 25-SEP-92 40800 1500 3333 Susan Ash AP 05-FEB-00 25250 500 4444 Stephen York CM 03-JUL-97 42420 2000 5555 Richard Jones CI 30-OCT-92 52500 2000 6666 Joanne Brown IN 18-AUG-94 49440 2000 7777 Donald Brown CI 05-NOV-99 45000 8888 Paula Adams IN 12-DEC-98 46350 2000 Record 6666 has a jobcode of IN so the answer to the ELSIF was true and the salary was increased by 3%.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.