Connecting Tech Pros Worldwide Forums | Help | Site Map

PL/SQL-PROCEDURES - 3

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,569
#1   May 31 '07
SAMPLE PROCEDURE USING REF-CURSOR
====================================
Expand|Select|Wrap|Line Numbers
  1. create or replace procedure refex(mnum  number)
  2. as
  3. --ref cursor is declared.
  4. type c1 is ref cursor;
  5. mname varchar2(10);
  6. mid number(4);
  7. --vc1 is a variable of type c1 ,which is a ref cursor.
  8. vc1 c1;
  9. begin
  10. --depending on the user input the procedure selects data dynamically from separate tables using ref cursor..
  11. if mnum=1 then
  12. open vc1 for select empno,ename from emp;
  13. elsif mnum=2 then
  14. open vc1 for select deptno,dname from dept;
  15. end if;
  16. loop
  17. fetch vc1 into mid,mname;
  18. exit when vc1%notfound;
  19. dbms_output.put_line(mid ||' '|| mname);
  20. end loop;
  21. --close the ref cursor.
  22. close vc1;
  23. end;
  24.  
SAMPLE EXAMPLE OF PROCEDURE WITH IN OUT MODE
=============================================

Expand|Select|Wrap|Line Numbers
  1. --the same variable receive the value and returns the value after prcessing.
  2. CREATE OR REPLACE procedure fact(a in out number)
  3. is
  4. b number:=1;
  5. begin
  6. for i in 1..a loop
  7. b:=b*i;
  8. end loop;
  9. a:=b;
  10. end;
  11.  
TO EXECUTE THE PROCEDURE FROM AN ANONYMOUS BLOCK
================================================== =
Expand|Select|Wrap|Line Numbers
  1. declare
  2. x number;
  3. begin
  4. x:=&values;
  5. --x is the input variable
  6. fact(x);
  7. --same x is the output also.
  8. dbms_output.put_line(x);
  9. end;
  10.  
Note :--since this procedure contains both IN and OUT mode parameter ,it can't be executed directly at SQL prompt using exec.

Sample Example Of Procedure With In ,out And In Out Mode
================================================== =======
Expand|Select|Wrap|Line Numbers
  1. Create Or Replace Procedure Ioio
  2. (
  3. Num Emp.empno%type,
  4. Name Out Emp.ename%type,
  5. Num1 In Out Number
  6. )
  7. As
  8. Begin
  9. Select Ename,sal Into Name,num1 From Emp Where Empno=num And Mgr =num1;
  10.  Dbms_output.put_line(name||'   '||num1);
  11. End;
  12.  
To Execute
===============
Expand|Select|Wrap|Line Numbers
  1. Declare
  2. N Varchar2(10);
  3. N1 Int :=7566;
  4. Begin
  5. Ioio(7788,n,n1);
  6. End;
  7.  
SAMPLE TO SHOW CALLING A PROCEDURE WITHIN ANOTHER PROCEDURE
==================================================

Expand|Select|Wrap|Line Numbers
  1. create or replace procedure find(eno number, flag out boolean)
  2. is
  3. a number(2);
  4. begin
  5. --finds out the number of records in the table where empno = the user input.
  6. select count(*) into a from emp where empno=eno;
  7. if a=0 then
  8. --if there is no such reacord already .set flag to FALSE.
  9. flag:=FALSE;
  10. else
  11. --else to TRUE.
  12. flag:=TRUE;
  13. end if;
  14. end;
  15.  
THIS PROCEDURE CALLS THE PREVIOUS ONE
======================================
Expand|Select|Wrap|Line Numbers
  1. create or replace procedure ins_emp(a number)
  2. is
  3. eno emp.empno%type;
  4. fg boolean;
  5. begin
  6. --the input of this procedure is passed to the previous procedure.
  7. eno:=a;
  8. --abc is a label.
  9. <<abc>>
  10. --the previous procedure is called here.
  11. find(eno,fg);
  12. --depending on the existance of the record the previosu procedure set the flag variable to TRUE  or FALSE
  13. if fg=true then
  14. --if flag is true then display the message 
  15. dbms_output.put_line(eno||'  No already exists...');
  16. --and increment the input value by 1 
  17. eno := eno+1;
  18. --and go back to the label and keep trying in a loop till the value is not in the table.
  19. goto abc;
  20. else
  21. --if flag is false insert the vale to the table.
  22. insert into emp(empno) values(eno);
  23. dbms_output.put_line('New empno inserted is : '||eno);
  24. end if;
  25. end;
  26.  
Note :--since this procedure contains only IN mode parameter ,it can be executed directly at SQL prompt.

Last edited by debasisdas; Apr 3 '08 at 01:00 PM. Reason: added stuff



Reply