473,421 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,421 developers and data experts.

PL/SQL-PROCEDURES - 3

debasisdas
8,127 Expert 4TB
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.
May 31 '07 #1
0 7918

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
2
by: Peter | last post by:
I run most of my SQL scripts via kornshell on AIX. I use the "here-document" to run some of the smaller ones. Example: #!/bin/ksh # Analyze the table. sqlplus...
0
by: Jan | last post by:
I store sql-commands in a database table. In the first step I get the sql command out of the database table with embedded sql. In the second step I try to execute the command, which i got from the...
2
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000...
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
4
by: coosa | last post by:
Hi, I was installing SQL Server on my machine and during installation my PC freezed. It happens frequently on my machine. So i tried after restarting to install it again and since then i always...
1
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005...
6
by: Fuzzydave | last post by:
I am back developing futher our Python/CGI based web application run by a Postgres DB and as per usual I am having some issues. It Involves a lot of Legacy code. All the actual SQL Querys are...
14
by: Developer | last post by:
Hello All, i have recently installed VS2005 and was trying to install SQL sever 2000. I have Win XP' SP2. But when I tried installing, it only installed client tools and not the database. Can...
5
by: dbrother | last post by:
Access 2003 Win XP Pro SP3 Using SQL /ADO Recordsets in a Do Loop Hello, I'm using a random number generator based on an integer input from a user from a form that will get X number of random...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.