Procedure in PL/SQL Example: 1
Procedure in PL/SQL Example: 1
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.
SQL> begin
2
3 P_GREETINGS;
4
5 end;
6 /
HELLO...EVERYONE...
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
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.
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
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
Commit complete.
Example: 4
Below example will show the usage of parameter notations such as Positional
parameter, named parameter and mixed parameter.
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
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
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
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
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.