This document provides exercises for learning PL/SQL. It includes examples of:
1) Declaring variables and using them to calculate and display the square of a user-provided number.
2) Writing an executable block to interchange salaries of two employees.
3) Using a cursor to display the name of the first employee hired.
4) Using loops and conditional statements to display selected employees.
5) Examples of procedures, functions, exceptions, triggers and other PL/SQL concepts.
Download as DOC, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2K views
PL SQL Exercises
This document provides exercises for learning PL/SQL. It includes examples of:
1) Declaring variables and using them to calculate and display the square of a user-provided number.
2) Writing an executable block to interchange salaries of two employees.
3) Using a cursor to display the name of the first employee hired.
4) Using loops and conditional statements to display selected employees.
5) Examples of procedures, functions, exceptions, triggers and other PL/SQL concepts.
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7
1
PL-SQL Course Exercises
Written By Eli L. 09-2013. 1 - Declarin PL!SQL "#enti$iers Use PLSQL Variables In this exercise, you will calculate the square of a number. The value of the number will be provided with the help of a substitution variable. Then the result will be displayed on the screen. Create the following PL!"L script# SET SERVEROUTPUT ON DECLARE v_num NUMBER := &sv_num; v_result NUMBER; BEGN v_result := PO!ER"v_num# $%; DBMS_OUTPUT&PUT_LNE "'T(e v)lue *+ v_result ,s: '-- v_result%; END; $xecute the script, and then answer the following questions# a% If the value of v_num is equal to &', what output is printed on the screen( b% )hat is the purpose of using a substitution variable( c% )hy is it considered a good practice to enclose substitution variables with single quotes for string data types( 2 - Writin Executa%le State&ents &. Complete the plsql bloc* and )rite a program to interchange the salaries of employee &+' and &++ in the ,- schema. De.l)re V_s)l)r/_0$1 22 use 3T4PE; V_s)l)t/_0$$ 22 use 3T4PE; Be5,n 67 8ut (ere t(e SELECT ,nt* STATEMENTS 76 U89)te em8l*/ees set s)l)r/ = V_s)l)r/_0$$ !(ere em8l*/ee_,9 = 0$1; U89)te em8l*/ees 2 set s)l)r/ = v_s)l)r/_0$1 !(ere em8l*/ee_,9 = 0$$; C*mm,t; 67 ADD )n9 e:.e8t,*n ;l*.< 76 En9; 3 - "nteractin 'it( t(e )racle Ser*er .isplay The /ob Title and 0ame of the $mployee who 1oined the 1ob first day. De.l)re Curs*r =*;s.ur ,s 67 .*m8lete t(e .urs*r 9e+,n,t,*n 76
en9; +. .isplay 2th and &'th employees in $mployees table. 9e.l)re .urs*r em8.ur ,s sele.t em8l*/ee_,9# +,rst_n)me +r*m em8l*/ees;
;e5,n +*r em8re. ,n em8.ur l**8 ,+ em8.ur3r*?.*unt A B t(en 9;ms_*ut8ut&8ut_l,ne" em8re.&+,rst_n)me%; e:,t ?(en em8.ur3r*?.*unt A 01; en9 ,+; en9 l**8;
en9; 3 Case statement: SET SERVEROUTPUT ON DECLARE v_9)te DATE := TO_DATE"'&sv_user_9)te'# 'DD2MON24444'%; v_9)/ VARCCAR$"0%; BEGN v_9)/ := TO_CCAR"v_9)te# 'D'%; CASE v_9)/ !CEN '0' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s Sun9)/'%; !CEN '$' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s M*n9)/'%; !CEN 'D' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s Tues9)/'%; !CEN 'B' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s !e9nes9)/'%; !CEN 'E' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s T(urs9)/'%; !CEN 'F' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s >r,9)/'%; !CEN 'G' TCEN DBMS_OUTPUT&PUT_LNE "'T*9)/ ,s S)tur9)/'%; END CASE; END; $xecute the script, and then answer the following questions# a)If the value of v_9)te equals 3&24/504+''+3, )hat output is printed on the screen( 6% ,ow many times v7day is calculated( 4 ,- Wor-in 'it( Co&.osite Data /y.es ,ere is an example of a record type in an anonymous PL!"L bloc*. SET SERVEROUTPUT ON; DECLARE vr_em8 EMPLO4EES3RO!T4PE; BEGN SELECT 7 NTO vr_em8 >ROM EMPLO4EES !CERE r*?num H $; DBMS_OUTPUT&PUT_LNE"'>,rst N)me: '--vr_em8&+,rst_n)me%; DBMS_OUTPUT&PUT_LNE"'S)l)r/: '--vr_em8&s)l)r/%; DBMS_OUTPUT&PUT_LNE"'Em),l: '--vr_em8&em),l%; END; -un in !"L PL8! 4 )hat will happen when the preceding example is run in a !"L9Plus session( 0 - 1sin Ex.licit Cursors Create a PL-SQL block using an explicit cursor that prints all Employee name, jobs and salaries for salary aboe aerage salary! 2 - Creatin Store# Proce#ures 5 Create a procedure that ta*es department I. and changes the manager I. for the department to the employee in the department with highest salary. :8se $xceptions%. .re)te *r re8l).e 8r*.e9ure .()n5e_9e8t_m)n)5er"9e8t,9 num;er% ,s v_em8,9 67 USE )88r*8r,)te 3t/8e 76; ;e5,n sele.t em8l*/ee_,9 ,nt* v_em8,9 +r*m em8l*/ees ?(ere s)l)r/ = " 67 !RTE NESTET S@L st)tement 76% )n9 9e8)rtment_,9 = 9e8t,9; u89)te 67 ?r,te u89)te st)tement I 76 en9; 9 - Creatin Store# 3unctions Create a function that ta*es department I. and returns the name of the manager of the department. .re)te *r re8l).e +un.t,*n 5et_9e8t_m)n)5er_n)me"9e8t,9 num;er% return v)r.()r ,s v_n)me 67 PUT 3t/8e e:8ress,*n 76; ;e5,n sele.t +,rst_n)me ,nt* v_n)me +r*m em8l*/ees ?(ere em8l*/ee_,9 = " 67 ?r,te t(e neste9 SELECT st)tement 76 %; return v_n)me; en9; 4 Exce.tions5 6 "dd exceptions to procedure and function in preious exercises and test cases #here $ept! %$ is &'( found 10 - Creatin Pac-aes 11 - Wor-in 'it( Pac-aes 12 - 1sin )racle-Su..lie# Pac-aes in 6..lication De*elo.&ent 13 - 1sin Dyna&ic SQL 1+ - Desin Consi#erations $or PL!SQL Co#e 1, - Creatin /riers $nsure no changes can be made to $;PL<=$$! table before >am and after &'pm in a day. .re)te *r re8l).e tr,55er tr5_em8l*/ees_t,me_.(e.< ;e+*re u89)te *r ,nsert *r 9elete *n em8l*/ees +*r e).( r*? ;e5,n 67 .*9e t(e )88r*8r,)te ,+ st)tement 76 en9; &. Create a Trigger to ensure the salary of the employee is not decreased. .re)te *r re8l).e tr,55er tr5_em8l*/ees_s)l)r/_.(e.< ;e+*re u89)te *n em8l*/ees +*r e).( r*? ;e5,n 67 ?r,te t(e )88r*8r,)te ,+ st)tement 76 en9; 10 - Creatin Co&.oun#7 DDL7 an# E*ent Data%ase /riers 14 - 1sin t(e PL!SQL Co&.iler 12 - 8anain PL!SQL Co#e 19 - 8anain De.en#encies 7 d)