You are on page 1of 19

PL/SQL

By, Shrinivas Achalkar

FEATURES  PL/SQL Block Structure PL/SQL Variables and Constants   Conditional Control Iterative Control Cursor   .

.declarations] BEGIN -.statements [EXCEPTION -.handlers] END.PL/SQL BLOCK STRUCTURE  [DECLARE -.

such as CHAR. part_price NUMBER(6. variable-size arrays. part_name VARCHAR2(20). or aPL/SQL-only datatype.PL/SQL VARIABLES AND CONSTANTS  Declaring Variables   Variables can have any SQL datatype. and records using the TABLE. . in_stock BOOLEAN. VARRAY. or NUMBER. DATE.  Can also declare nested tables. such as BOOLEAN or PLS_INTEGER.2). Example: DECLARE part_no NUMBER(6). and RECORD composite datatypes.

Useful when declaring variables that will hold database values.DECLARING VARIABLES  %TYPE  Provides the datatype of a variable or database column. Example: v_last_name employees.   .last_name%TYPE.

BEGIN SELECT * INTO dept_rec FROM departments. END .  Example:  DECLARE dept_rec departments%ROWTYPE. DBMS_OUTPUT.deptname).PUT_LINE(dept_rec.DECLARING VARIABLES  %ROWTYPE %ROWTYPE attribute provides a record type that represents a row in a table.  Columns in a row and corresponding fields in a record have the same names and datatypes.

DBMS_OUTPUT. emp_rec employee%ROWTYPE.  Eg.ename). BEGIN SELECT * INTO emp_rec FROM employee WHERE emp_id=&empid.PUT_LINE(emp_rec.  DECLARE empid number.&VARIABLE Is a bind variable. END .  Used to take value from user at run time.

Using INTO keyword in SELECT statement  Example: DECLARE bonus NUMBER(8. END. .10 INTO bonus FROM employees WHERE employee_id = emp_id. Using assignment operator(:=)   Example: hours_worked NUMBER := 40.2).PL/SQL VARIABLES AND CONSTANTS Assigning Values to a Variable  3 ways: 1. emp_id NUMBER(6) := 100. 2. BEGIN SELECT salary * 0.

By passing variable as an OUT or IN OUT parameter to subprogram. then assigning value Declaring Constants o o Example: o credit_limit CONSTANT NUMBER := 5000.PL/SQL VARIABLES AND CONSTANTS o 3.00. .

BIND VARIABLES You do need to specify bind variables with dynamic SQL i. bind variables are used to take value from user at run time  Syntax  .e.

[ELSE Statements.CONDITIONAL CONTROL  IF statement:  Syntax: IF condition THEN Statements. [ELSEIF condition THEN Statements.]] END IF .

 Eg.CONDITIONAL CONTROL  CASE Statement:  Syntax: CASE WHEN condition THEN Statements. WHEN job_id=‘st_clerk’ THEN .[n] ELSE Statements. …. END CASE  Condition can only be eqality condition.

you must write EXIT WHEN statement.sequence of statements END LOOP. Eg.ITERATIVE CONTROL  Endless LOOP:  LOOP -. LOOP emp_id:=emp_id+1. END LOOP  . EXIT WHEN empid > 10.    For endless loop to terminate.

 END LOOP   Eg.ITERATIVE CONTROL  WHILE LOOP:  Syantax: WHILE condition LOOP  Statements. WHILE emp_id < 10 LOOP DBMS_OUTPUT.PUT_LINE(emp_id). END LOOP . emp_id:=emp_id+1.

. End LOOP Statements.  Instead of start . End. no need to declare ctrl_var.ITERATIVE CONTROL  FOR LOOP:   Is an optimized for loop for cursor so that a record variable need not be created. .  .  Its read only variable. Syntax: FOR ctrl_var IN start . ypu can write SQL query. END LOOP Here.  Inside loop. you can’t assign value to ctrl_var.

.first_name ||’. END LOOP. END. Last name = ' || someone. Eg.PUT_LINE('First name = ' || someone.last_name). BEGIN FOR someone IN (SELECT * FROM employees WHERE employee_id < 120 ) LOOP DBMS_OUTPUT.

 It creates a stream between session and oracle process i.  When the last row is fetched .CURSOR Is a memory object.implicitly the cursor is closed so that the memory occupied by cursor gets released. database.  It fetch the data row by row from table and passes it to session.  Types:  Implicit cursor  Explicit cursor  .e.

 DECLARE CURSOR c1 IS SELECT * FROM Employee ORDER BY emp_id. Implicit cursor name is ‘sql’.CURSOR  Implicit cursor:   Created when DML statement is fired i.  Explicit cursor: Used to retrive multiple rows. DELETE. the query isn’t get fired. When u declare a cursor. UPDATE. INSERT.  Eg.  The query get fired when u fetch 1st row into record type.e.  .

THANK YOU .

902039 O $39.343-006.9 .43/943  4-*/ 89*.43/943% $9. %   %#  $$9.9020398 3( $ $9..07 % . $ .9020398 $ O 43/943.

04189.902039 O    ! 025*/025*/  %025/    ! O .90 4:2:89790 O %89.9020398  O 4703/0884459490723.%#%'  %#  3/088 O !  ! ! 806:03.

9020398  ! O  025*/   ! $* &%!&% !&%* 025*/  025*/025*/   ! . .43/943 $9.%#%'  %#   O ! ! $.39.

345920/147445147.90/  O $39.70.../4189.-0300/349-0..%#%'  %#   # O ! 8.3790$"6:07 O .3 9.70..79  3/ 5: ./43..7 89.-0 O 3890.883.97*.7.7 O 38/0445 4:.97*.  #.:7847849.7.47/ ...79  3/ $9.70.7 O 9870.9.97*.:094.9020398  ! ! 070 34300/94/0...

89*3.20  8420430 .20  .20  8420430 1789*3.20    ! .O    #8420430 $% # 0254008# 025400*/    ! $* &%!&% !&%* 7893.893.

80 9109.90/.8970.:50/- .0 574.908..3/47.9 9.:7847 O .9.-.0/ 25.89748109.9.2-09003808843.990202474.3/ 5.:7847098700.74-7417429.70.202474-0.&#$ # 8.:7847 O 5.-0.80/ %508 25.990 .9..480/849.088 0 /.9.:78478.8808994808843 0390.

9020398170/ 0 $#%  &!% % O 25.89 7439470..0 2:950748 O  O # &#$ #.:7847 70.70.208 86 O 5.:7847 &80/947097.9.90/0389..47/ 950 O .9.&#$ # 25.9.:78473.:7847 906:0783 909170/ O %06:0709170/03:109.$ $% # 25400 ##025*/ 03:/0.

% & .