0% found this document useful (0 votes)
122 views9 pages

Procedure in PL/SQL Example: 1

The document discusses examples of using procedures in PL/SQL. It covers: 1) Creating a simple procedure that displays a greeting message. 2) Creating a procedure that accepts two numbers as parameters, adds them, and displays the result. 3) Creating a procedure that accepts a city parameter, queries an employee table, and displays names of employees in that city. 4) Demonstrating the use of positional, named, and mixed parameter notation when calling procedures. 5) Creating a procedure with IN and OUT parameter types.

Uploaded by

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

Procedure in PL/SQL Example: 1

The document discusses examples of using procedures in PL/SQL. It covers: 1) Creating a simple procedure that displays a greeting message. 2) Creating a procedure that accepts two numbers as parameters, adds them, and displays the result. 3) Creating a procedure that accepts a city parameter, queries an employee table, and displays names of employees in that city. 4) Demonstrating the use of positional, named, and mixed parameter notation when calling procedures. 5) Creating a procedure with IN and OUT parameter types.

Uploaded by

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

PROCEDURE IN PL/SQL

Example: 1
SQL> CREATE OR REPLACE PROCEDURE P_GREETINGS
2 IS
3
4 BEGIN
5
6 DBMS_OUTPUT.PUT_LINE('HELLO...EVERYONE...');
7
8 END P_GREETINGS;
9 /
Procedure created.

In this example procedure P_GREETINGS is created which will display following


output when we call this procedure.

SQL> begin
2
3 P_GREETINGS;
4
5 end;
6 /
HELLO...EVERYONE...

PL/SQL procedure successfully completed.


Commit complete.

Another way to execute this procedure is as follow,


SQL> execute P_GREETINGS;
HELLO...EVERYONE...

PL/SQL procedure successfully completed.


Commit complete.
Example: 2

SQL> create or replace procedure p_add(p_num1 number,p_num2 number)


2 is
3
4 v_ans number;
5
6 begin
7
8 v_ans:=p_num1+p_num2;
9
10 dbms_output.put_line(v_ans);
11
12 end p_add;
13 /

Procedure created.
In this above procedure we have passed two parameters p_num1 and p_num2.
This procedure is performing addition operation of two numbers and display the
result in v_ans variable.
Below code is used to call the procedure by passing two variables v_num1 and
v_num2.

SQL> declare
2
3 v_num1 number;
4 v_num2 number;
5
6 begin
7
8 v_num1:=&v_num1;
9 v_num2:=&v_num2;
10
11 p_add(v_num1,v_num2);
12
13 end;
14
15 /
Enter value for v_num1: 50
old 8: v_num1:=&v_num1;
new 8: v_num1:=50;
Enter value for v_num2: 80
old 9: v_num2:=&v_num2;
new 9: v_num2:=80;
130

PL/SQL procedure successfully completed.

Commit complete.
SQL>

Example: 3

Below example is created to display employee name based on the city name passed
in the parameter of the procedure.

SQL> select * from tbl_emp;

EMPID EMPNAME EMPCITY


---------- -------------------- --------------------
12 kirtan ahmedabad
13 yash delhi
15 rajesh surat
16 ram ahmedabad

SQL> create or replace procedure p_dispemp(p_city tbl_emp.empcity%type)


2 is
3
4 v_empname tbl_emp.empname%type;
5
6 cursor c1 is
7 select empname
8 from tbl_emp
9 where empcity = p_city;
10
11 begin
12
13 open c1;
14
15 loop
16
17 fetch c1 into v_empname;
18 exit when c1%notfound;
19
20 dbms_output.put_line('Employee name is : '|| v_empname);
21
22 end loop;
23
24 close c1;
25
26 end p_dispemp;
27 /

Procedure created.
The below code is used to call the procedure by passing city name in the
parameter.

SQL> declare
2
3 v_city tbl_emp.empcity%type;
4
5 begin
6
7 v_city:='&v_city';
8
9 p_dispemp(v_city);
10
11 end;
12 /
Enter value for v_city: delhi
old 7: v_city:='&v_city';
new 7: v_city:='delhi';
Employee name is : yash

PL/SQL procedure successfully completed.

Commit complete.
SQL> /
Enter value for v_city: ahmedabad
old 7: v_city:='&v_city';
new 7: v_city:='ahmedabad';
Employee name is : kirtan
Employee name is : ram

PL/SQL procedure successfully completed.

Commit complete.
Example: 4

Below example will show the usage of parameter notations such as Positional
parameter, named parameter and mixed parameter.

SQL> create or replace procedure p_position(p_x number,p_y char,p_z number)


2 is
3
4 begin
5
6 dbms_output.put_line('value of x is...'||p_x);
7
8 dbms_output.put_line('value of y is...'||p_y);
9
10 dbms_output.put_line('value of z is...'||p_z);
11
12 end p_position;
13
14 /

Procedure created.

SQL> declare
2
3 v_one number;
4
5 v_two char;
6
7 v_three number;
8
9 begin
10
11 v_one:=50;
12 v_two:='K';
13 v_three:=70;
14
15 p_position(v_one,v_two,v_three);
16
17 dbms_output.put_line('Now...change the position...');
18
19 p_position(v_three,v_two,v_one);
20
21 end;
22 /
value of x is...50
value of y is...K
value of z is...70
Now...change the position...
value of x is...70
value of y is...K
value of z is...50

PL/SQL procedure successfully completed.

Commit complete.
SQL>

In the above example only we have change the position so, depending on the
datatype of each parameter existed in the procedure definition the calling
arguments will match with it and the value will be copied into the parameter.
i.e. v_one and p_x both are of same data type so if we have change the position
than also it will match only the datatype of both the parameter i.e. actual and
calling parameters.

SQL> declare
2
3 v_one number;
4
5 v_two char;
6
7 v_three number;
8
9 begin
10
11 v_one:=50;
12
13 v_two:='K';
14
15 v_three:=70;
16
17 p_position(v_one,p_z=>v_three,p_y=>v_two);
18
19 end;
20
21 /
value of x is...50
value of y is...K
value of z is...70

PL/SQL procedure successfully completed.

Commit complete.

In the above example we have used => notation to indicate the name of the actual
parameter with the calling parameter.
So if we change the position of variable v_three and v_two as shown above in the
line number 17, we will get the same result.

Example: 5

SQL> create or replace procedure p_type(p_one in number, p_two out number)


2 is
3
4 begin
5
6 dbms_output.put_line('1st parameter is..'||p_one);
7
8 dbms_output.put_line('2nd parameter is..'||p_two);
9
10 p_two:=50;
11
12 end p_type;
13
14 /

Procedure created.
In the above example 1st parameter is of IN type so read only value and 2nd is OUT
type so write-only value.

SQL> declare
2
3 v_one number:=100;
4
5 v_two number:=200;
6
7 begin
8
9 dbms_output.put_line('Two variables are..'||v_one||' and '||v_two);
10
11 p_type(v_one,v_two);
12
13 dbms_output.put_line('Now...Two variables are..'||v_one||' and '||v_two);
14
15 end;
16
17 /
Two variables are..100 and 200
1st parameter is..100
2nd parameter is..
Now...Two variables are..100 and 50

PL/SQL procedure successfully completed.

Commit complete.
In this above example p_one is having a value 100 from the v_one variable which
is read only type parameter but, p_two will get no value from the calling statement
but the value will be changed i.e. p_two will be 50 and so v_two will get 50.

You might also like