Unit IV
Unit IV
PL/SQL PROGRAMMING
Introduction :
Logical Expressions :
Operators:
Operators Descriptions
AND AND operations
OR Or Operation
NOT NOT Operation
PL/SQL Expressions and comparisons
1. Range Comparison Expressions :
Operators:
Operators Description
IN Use to check variable is equivalent with multiple
available values given in IN expression
BETWEEN Used to check variable is equivalent with given
range of values.
Begin
update emp set salary= salary+1000 where
dept_no = 101;
if SQL%FOUND then
DBMS_output.put_line(‘Salary Updated’);
else
DBMS_output.put_line(‘Salary Not Updated’);
end if
END
Explicit Cursor
The cursors which declared by users is called as Explicit
Cursor.
Steps to Declare Cursor:
Step 1: Declare the cursor in declaration cursor.
Example : CURSOR c_emp IS select empno, ename, salary
from emp;
Step 2 : Open the cursor in execution section.
Example : OPEN cursor_name;
Step 3: FETCH the data from cursor into PL/SQL variable or
records in the execution section.
Example : FETCH cursor_name into record name;
Step 4: CLOSE the cursor in the execution section before we
end the PL /sql BLOCK.
Cursor for loop
When ‘for’ loop is used in cursor we do not have to declare a
record of variables to store the cursor values.
Syntax :
FOR record IN cursor_name
LOOP
action 1
action 2
END LOOP
Parameterized Cursor
Parameterized cursors are static cursors that can accept passed in
parameter values when they are opened.
Example :
DECLARE
cursor c(no number) is select * from emp_information where
emp_no = no;
tmp emp_information%rowtype;
BEGIN
OPEN c(4);
FOR tmp IN c(4) LOOP
dbms_output.put_line('EMP_No: '||tmp.emp_no);
dbms_output.put_line('EMP_Name: '||tmp.emp_name);
dbms_output.put_line('EMP_Dept: '||tmp.emp_dept);
dbms_output.put_line('EMP_Salary:'||tmp.emp_salary);
END Loop;
CLOSE c;
END;
Procedures
Stored procedure is a group of SQL statement which can be
executed repeatedly.
The parameters makes the stored procedure more flexible and
useful. Stored procedures can increase productivity by writing once
and using it many times.
Syntax :
Create procedure procedure_name
[(param1, param 2])
IS
declaration section;
BEGIN
execution section;
EXCEPTION
exception section;
Advantages
Stored procedure can share logic with different
applications.
Stored procedure can isolate users from data table.
Stored procedure can provide security that means users
can input data and can also they can change it but they
could not write procedure.
Stored procedure can be used to improve performance.
Stored procedure is fast because it is precompiled.
It improves reliability and reduce development time.
Reduce network usage among servers and clients.
Executing Stored Procedure
Using EXECUTE ;
EXECUTE proc1;
Using CALL :
CALL proc1;