Procedureand Functions
Procedureand Functions
AIM
PROCEDURES
A stored procedure or in simple a proc is a named PL/SQL block which performs one or
more specific task. A procedure has a header and a body. The header consists of the name of the
procedure and the parameters passed to the procedure. The body consists of the declaration
section, execution section and exception section. A procedure may or may not return any value.
FUNCTIONS
A function is a named PL/SQL Block which is similar to a procedure. The major difference
between a procedure and a function
SYNTAX
PROCEDURE
To create
To execute
Procedures in PL/SQL:
Example 1:
SQL> set serveroutput on
SQL> CREATE OR REPLACE PROCEDURE welcome_msg (p_name IN VARCHAR2)
IS
BEGIN
dbms_output.put_line ('Welcome'|| p_name);
END;
/
Procedure created.
Example 2:
SQL> create table userinfo(id int,name varchar(20));
Table created.
SQL> create or replace procedure insert_user (id IN NUMBER, name IN VARCHAR2)
is
begin
insert into userinfo values(id,name);
dbms_output.put_line('record inserted successfully');
end;
/
Procedure created.
To call the procedure
SQL> exec insert_user(101,'raghul');
record inserted successfully
PL/SQL procedure successfully completed.
ID NAME
---------- --------------------
101 raghul
Example 3:
SQL>
create or replace PROCEDURE findMin(x IN number, y IN number, z OUT number) IS
BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
dbms_output.put_line('Minimum value '|| z);
END;
/
Minimum of (23, 45) : 23
Example 4:
SQL> DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS
BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a);
END;
/
Square of (23): 529
FUNCTION
To create
SQL> DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;
RETURN z;
END;
BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;
/
Maximum of (23,45): 45
Example 2:
Function created.
SQL> DECLARE
answer number;
BEGIN
answer:= totalstrength();
dbms_output.put_line('Total Strength. of Students is: ' || answer);
END;
/
Total Strength. of Students is: 100
AIM
CURSOR in PL/SQL
A cursor is a handle or pointer to this context area. PL/SQL allows the programmer to control the
context area and what happens to it as the statement is processed.
SYNTAX
Declaring a cursor
Syntax:
DECLARE
/* Cursor declaration*/
BEGIN
OPEN cursor_name;
.
CLOSE cursor_name;
END;
1. Declare Cursor: In this part we declare variables and return a set of values.
2. Open: This is the entering part of the cursor.
3. Fetch: Used to retrieve the data row by row from a cursor.
4. Close: This is an exit part of the cursor and used to close a cursor.
5. Deallocate: In this part we delete the cursor definition and release all the system
resources associated with the cursor
Example:
SQL> create table emp_det (id integer,name varchar2(10),age integer,address
varchar2(20),salary integer);
Table created.
Explicit cursors:
Example 2:
SQL> DECLARE
e_id emp_det.id%type;
e_name emp_det.name%type;
e_addr emp_det.address%type;
CURSOR c_employees is
BEGIN
OPEN c_employees;
LOOP
END LOOP;
CLOSE c_employees;
END;
Result:
AIM
SYNTAX
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
END;
Program 2:
SQL> DECLARE
a int:=10;
b int:=0;
answer int;
BEGIN
answer:=a/b;
exception
END;
the value of a is 10
the value of b is 0
RESULT
Thus the PL/SQL program that handles exception has been implemented and output was
verified.
AIM
SYNTAX
Create or replace Trigger< Trigger name> [before/after]
[when<condition>]
begin
SQL statements;
end
Table created.
begin
:new.tot:=:new.m1+:new.m2+:new.m3;
:new.avrg:=:new.tot/3;
:new.result:='pass';
else
:new.result:='Fail';
end if;
end;
12 /
Trigger created.
SQL> insert into stdnvalues(101,'SM',67,89,99,'','','');
1 row created.
RESULT