0% found this document useful (0 votes)
56 views

Without Parameter Program: Procedure

1. The document describes several PL/SQL procedures that demonstrate different ways to pass parameters, including using IN, OUT, and IN OUT parameters. 2. One procedure shows how to handle a divide by zero exception using an IF statement. 3. Other procedures demonstrate calculating a factorial using an OUT parameter, doubling a value using an IN OUT parameter, and swapping two values using IN OUT parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views

Without Parameter Program: Procedure

1. The document describes several PL/SQL procedures that demonstrate different ways to pass parameters, including using IN, OUT, and IN OUT parameters. 2. One procedure shows how to handle a divide by zero exception using an IF statement. 3. Other procedures demonstrate calculating a factorial using an OUT parameter, doubling a value using an IN OUT parameter, and swapping two values using IN OUT parameters.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

PROCEDURE

WITHOUT PARAMETER
PROGRAM
1 create or replace procedure example is
2 a number :=0;
3 b number :=2;
4 c number(2);
5 begin
6 c:=b/a;
7 exception
8 when zero_divide then
9 dbms_output.put_line('divide by zero');
10* end example;

OUTPUT
SQL> exec example;
divide by zero
PL/SQL procedure successfully completed.

WITH IN AND OUT PARAMETER


PROGRAM
Procedure for a factorial
SQL> get z:\file\p1m.sql;
1 create or replace procedure fact(n in number, f out number)
2 is
3 i number;
4 begin
5 f:=1;
6 for i in 2..n loop
7 f:=f*i;
8 end loop;
9* end;
SQL> /
Procedure created.

program to call factorial procedure


SQL> get z:\file\p1s.sql;
1 declare
2 f number;
3 n number:=&n;
4 begin
5 fact(n,f);
6 dbms_output.put_line('Factorial of ' || n || 'is : ' || f);
7* end;

OUTPUT
SQL> /
Enter value for n: 5
old 3: n number:=&n;
new 3: n number:=5;
Factorial of 5is : 120
PL/SQL procedure successfully completed

WITH IN OUT PARAMETER


PROGRAM
Procedure using inout parameter
SQL> get z:\p2m.sql;
1 create or replace procedure doublen (n in out int) is
2 begin
3 n := n * 3;
4* end;
SQL> /
Procedure created.

program to call procedure which use inout parameter


SQL> get z:\p2s.sql;
1 declare
2 r int;
3 begin
4 r := 7;
5 dbms_output.put_line('before call r is: ' || r);
6 doublen(r);
7 dbms_output.put_line('after call r is: ' || r);
8* end;

OUTPUT
SQL> /
before call r is: 7
after call r is: 21
PL/SQL procedure successfully completed.

SWAP TWO NUMBERS


WITH IN OUT PARAMETER
PROGRAM
Procedure using inout parameter
SQL> get z:\s1.sql;
1 create or replace procedure swap(a in out number,b in out number)
2 is
3 t number;
4 begin
5 t:=a;
6 a:=b;
7 b:=t;
8* end;
SQL> /
Procedure created.

program to call procedure which use inout parameter


SQL> get z:\s2.sql;
1 declare
2 a number:=&a;
3 b number:=&b;
4 begin
5 dbms_output.put_line('before swap');
6 dbms_output.put_line('value of a :'||a);
7 dbms_output.put_line('value of b :'||b);
8 swap(a,b);
9 dbms_output.put_line('after swap');
10 dbms_output.put_line('value of a :'||a);
11 dbms_output.put_line('value of b :'||b);
12* end;

OUTPUT
SQL> /
Enter value for a: 1
old 2: a number:=&a;
new 2: a number:=1;
Enter value for b: 7
old 3: b number:=&b;
new 3: b number:=7;
before swap
value of a :1
value of b :7
after swap
value of a :7
value of b :1
PL/SQL procedure successfully completed.

You might also like