Professional Documents
Culture Documents
Write and Execute PL-SQL Functions and Procedure Programs
Write and Execute PL-SQL Functions and Procedure Programs
Whenever you issue a SQL Statement, Oracle opens an area of memory in which the command is parsed
and executed. This Area is called CONTEXT AREA.
The information (Rows) retrieved from the database table, which is available in context area, is known
as the ACTIVE SET.
A Cursor is a pointer to the current row in the ACTIVE SET.
There are two types of Cursors:
1. Implicit Cursors: Created, Managed & Erased by Oracle Automatically.
2. Explicit Cursors: Created & Named by the Programmer.
Privileges:
SQL> grant create procedure to user_name; (DBA)
SQL> Grant Execute on <procedure_name> to user_name (owner)
Data Dictionary Views:
User_procedures ( General Info )
User_source ( the text of pl/sql procedure)
Desc procedure_name ( IN, OUT, INOUT parameters list)
User_errors (to see all the compilation errors in a procedure).
OR
SQL> show error;
Show err;
Practice Exercise:
1. Using implicit cursor update the salary by an increase of 10% for all the records in
EMPLOYEES table, and finally display how many records have been updated. If no
records exist display the message None of the salaries were updated.
2. WAP to accept the employee_id and display all the details of employees.If employee
doesnot exist display the appreciate message
3. Using explicit cursor fetch the employee name, employee_id and salary of all the
records from EMPLOYEES table.
4. Using explicit cursor Insert the records from EMPLOYEES table for the columns
employee_id, Last_Name and salary for those records whose salary exceeds 2500 into a
new table TEMP_EMP.
5. Write a Program to add a record in Department table using Procedure
6. Write a Program to fetch data from Emp table using Procedure.
7. Demonstrate Declaring, Defining and Invoking a simple PL/SQL
function which will compute and return the reverse of a number.
8. Write a PL SQL program to demonstrate function with IN and OUT Parameters.
Instructions:
1. Write and execute the programs in Oracle SQL server.
2. Paste the snapshot of the output in input & output section.
1. Using implicit cursor update the salary by an increase of 10% for all the records in
EMPLOYEES table, and finally display how many records have been updated. If
no records exist display the message None of the salaries were updated.
DECLARE
z_empid emp.empno%TYPE;
z_empname emp.ename%TYPE;
z_salary emp.sal%TYPE;
CURSOR employee_cursor IS -- declaring a cursor
SELECT empno,
ename,
sal
FROM emp;
BEGIN
OPEN employee_cursor; -- opening the cursor
LOOP
FETCH employee_cursor -- fetching records from the cursor
INTO z_empid,
z_empname,
z_salary;
EXIT
WHEN employee_cursor%NOTFOUND;
IF (z_salary < 8000) THEN
z_salary:=z_salary*1.1;
dbms_output.Put_line(z_empid
|| ' '
|| z_empname
|| ' '
|| z_salary);
ELSE
dbms_output.Put_line('no record exixst');
END IF;
END LOOP;
CLOSE employee_cursor; --closing the cursor
END;
/
2. WAP to accept the employee_id and display all the details of employees. If
employee does not exist display the appreciate message
DECLARE
z_emp emp%rowtype;
eid number;
BEGIN
eid:=:eid;
select *
into z_emp
from emp
where empno = eid;
if(eid =z_emp.empno)then
dbms_output.Put_line(z_emp.empno
|| ' '
|| z_emp.ename
|| ' '
|| z_emp.sal);
else
dbms_output.Put_line('Employee with this ID does not exist');
end if;
end;
/
3. Using explicit cursor fetch the employee name, employee_id and salary of all the
records from EMPLOYEES table.
DECLARE
z_empid emp.empno%TYPE;
z_empname emp.ename%TYPE;
z_salary emp.sal%TYPE;
CURSOR employee_cursor IS -- declaring a cursor
SELECT empno,
ename,
sal
FROM emp;
BEGIN
OPEN employee_cursor; -- opening the cursor
LOOP
FETCH employee_cursor -- fetching records from the cursor
INTO z_empid,
z_empname,
z_salary;
EXIT
WHEN employee_cursor%NOTFOUND;
dbms_output.Put_line(z_empid
|| ' '
|| z_empname
|| ' '
|| z_salary);
END LOOP;
CLOSE employee_cursor; --closing the cursor
END;
/
4. Using explicit cursor Insert the records from EMPLOYEES table for the columns
employee_id, Last_Name and salary for those records whose salary exceeds 2500
into a new table TEMP_EMP.
DROP TABLE temp_emp;
CREATE TABLE temp_emp AS
SELECT empno,ename,sal
FROM emp;
DELETE FROM temp_emp;
COMMIT;
DECLARE
z_empid emp.empno%TYPE;
z_empname emp.ename%TYPE;
z_salary emp.sal%TYPE;
CURSOR cur_st IS
SELECT empno,
ename,
sal
FROM emp
WHERE sal > "2500";
BEGIN
OPEN cur_st;
LOOP
FETCH cur_st INTO z_empid,z_empname, z_salary;
EXIT WHEN cur_st%NOTFOUND;
INSERT INTO temp_emp
(empno,
ename,
sal)
VALUES (z_empid,
z_empname,
z_salary);
END LOOP;
CLOSE cur_st;
COMMIT;
END;
/
BEGIN
a := 123456789;
c := reverse_it(a);
dbms_output.put_line('the reverse of number is ' || c);
END;
Output:
the reverse of number is 987654321
Output:
Example of function:
RETURN total;
END;
/