0% found this document useful (0 votes)
22 views11 pages

Exp 9

Uploaded by

placementcell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views11 pages

Exp 9

Uploaded by

placementcell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment 9

Experiment 7 : Working with PL/SQL Procedures


Programs Development using Creation of Procedures, Passing Parameters IN and OUT of
Procedures

Procedural Language/Structured Query Language (PL/SQL) is an extension of SQL.


Basic Syntax of PL/SQL
DECLARE
/* Variables can be declared here */
BEGIN

/* Executable statements can be written here */


EXCEPTION
/* Error handlers can be written here. */
END;

As we want output of PL/SQL Program on screen, before Starting writing anything type (Only
Once per session)
SET SERVEROUTPUT ON

Ex :- PL/SQL to find addition of two numbers

DECLARE
A INTEGER := &A;
B INTEGER := &B;
C INTEGER;
BEGIN
C := A + B;
DBMS_OUTPUT.PUT_LINE ('THE SUM IS '||C);
END;
/
Output :
Decision making with IF statement :-

The general syntax for the using IF--ELSE statement is


IF (TEST_CONDITION) THEN
SET OF STATEMENTS
ELSE
SET OF STATEMENTS
END IF;

For Nested IF—ELSE Statement we can use IF--ELSIF—ELSE as follows

IF (TEST_CONDITION) THEN
SET OF STATEMENTS
ELSIF (CONDITION)
SET OF STATEMENTS
END IF;
Ex:- Largest of three numbers.
This program can be written in number of ways, here are the two different ways to write the
program.
1) Using IF-
ELSE DECLARE
A NUMBER := &A; B
NUMBER := &B; C
NUMBER := &C; BIG
NUMBER; BEGIN

IF (A > B) THEN BIG


:= A;

ELSE BIG

:= B;

END IF;
IF(BIG < C ) THEN DBMS_OUTPUT.PUT_LINE('BIGGEST OF A,
B AND C IS ' || C);
ELSE
DBMS_OUTPUT.PUT_LINE('BIGGEST OF A, B AND C IS ' || BIG);
END IF;
END;
/

Output :
2) Using IF—ELSIF—ELSE
DECLARE
A NUMBER := &A; B
NUMBER := &B; C
NUMBER := &C;
BEGIN

IF (A > B AND A > C) THEN


DBMS_OUTPUT.PUT_LINE('BIGGEST IS ' || A); ELSIF
(B > C) THEN DBMS_OUTPUT.PUT_LINE('BIGGEST IS ' ||
B); ELSE DBMS_OUTPUT.PUT_LINE('BIGGEST IS '

|| C);
END IF;
END;
/

Output :
Procedure in PL/SQL

Procedures are written for doing specific tasks. The general syntax of procedure is

CREATE OR REPLACE PROCEDURE <Pro_Name> (Par_Name1 [IN / OUT/ IN OUT]


Par_Type1, ….) IS (Or we can write AS)
Local declarations;

BEGIN
PL/SQL Executable statements;

..
..

..
EXCEPTION
Exception Handlers;
END <Pro_Name>;

Mode of parameters
1) IN Mode :- IN mode is used to pass a value to Procedure/Function. Inside the
procedure/function, IN acts as a constant and any attempt to change its value causes compilation
error.
2) OUT Mode : The OUT parameter is used to return value to the calling routine. Any attempt to
refer to the value of this parameter results in null value.
3) IN OUT Mode : IN OUT parameter is used to pass a value to a subprogram and for getting the
updated value from the subprogram.

To use/call procedure, write a PL/SQL code and include call in the code using
Pro_Name(Par_List);
Or you can execute from SQL Prompt as
execute Pro_Name(Par_List)

For dropping/deleting Procedure


DROP PROCEDURE Pro_Name;
Examples

1) Simple program to illustrate Procedure.


-- Assume file name P1
CREATE OR REPLACE PROCEDURE P1(A NUMBER)
AS BEGIN
DBMS_OUTPUT.PUT_LINE('A:'||A); END
P1;

Output :

Now write PL/SQL code to use procedure in separate file.


-- Assume file name testP1
DECLARE
BEGIN
P1(100); END;

Output :
2) Program to illustrate Procedure with IN mode
parameter. -- Assume file name P2
CREATE OR REPLACE PROCEDURE P2(A IN NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('A:'||A);
END P2;
/

Output :

-- Assume file name testP2


DECLARE X
NUMBER;
BEGIN X:=10;

DBMS_OUTPUT.PUT_LINE('X:'||X);
P2(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;

Output :
3) Program to illustrate Procedure with OUT mode parameter.
-- Assume file name P3
CREATE OR REPLACE PROCEDURE P3(A OUT NUMBER) AS
BEGIN
A:=100;
DBMS_OUTPUT.PUT_LINE('A:'||
A); END P3;
/

Output :

-- Assume file name testP3


DECLARE X
NUMBER;
BEGIN X:=50;

DBMS_OUTPUT.PUT_LINE('X:'||X);
P3(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;

Output :
4) Program to illustrate Procedure with OUT mode parameter.
-- Assume file name P4
CREATE OR REPLACE PROCEDURE P4(A OUT NUMBER) AS
BEGIN
DBMS_OUTPUT.PUT_LINE('A:'||A); END
P4;

Output :

-- Assume file name testP4


DECLARE X
NUMBER;
BEGIN X:=10;

DBMS_OUTPUT.PUT_LINE('X:'||X);
P4(X); DBMS_OUTPUT.PUT_LINE('X:'||
X); END;

Output :
5) Program to illustrate Procedure with IN OUT mode
parameter. --Assume file name P5
CREATE OR REPLACE PROCEDURE P5(A IN OUT NUMBER)
AS BEGIN
DBMS_OUTPUT.PUT_LINE ('A:' || A);
END P5;
/

Output :

-- Assume file name testP5


DECLARE X
NUMBER;
BEGIN X:=10;

DBMS_OUTPUT.PUT_LINE ('X:'|| X);


P5(X); DBMS_OUTPUT.PUT_LINE
('X:'|| X); END;

Output :

You might also like