Wednesday, 15 October 2014

How to return multiple value from a procedures / functions in PL/SQL

Generally what we know that functions return values but procedures not. But still take any programming language be it C++,  Visual Basic, Java you can always send parameters by-references who value can be modified inside functions. And such case can be explained as returning values from a procedures/ functions through parameters.

Same is applicable in PL/SQL procedures / functions.

I will be writing one test script which will consists of a procedure and a block from where this procedure will be called. The function will swap two numbers. I am not explaining the logic of swap procedure as I expect you know the logic.

declare
p number;
q number;

procedure swap(x out number,y out number)
tmp number;
begin
   tmp:= x;
   x:=  y;
   y:= tmp;

end swap;

begin
p:= 10;
q:= 20;
dbms_output.print_line("Before swap value of p = "||p||" value of q = "||q);
swap(p,q);
dbms_output.print_line("After swap value of p = "||p||" value of q = "||q); 
end;
--Just check out that value of x and y are swapped and as they are OUT parameter therefore value of p and q are also changed.

Two pass a variable by reference in PL/SQL you need to pass the variable with OUT parameter.

You can also watch the video on OUT parameter in Youtube : https://www.youtube.com/watch?v=5Qw26Xiwy7s

Also can subscribe to my channel : https://www.youtube.com/subhro190776



No comments: