473,611 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PL/SQL-PROCEDURES - 3

debasisdas
8,127 Recognized Expert Expert
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 7927

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

Similar topics

3
3343
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 about sql database and sql server, specially this article: http://www.aspfaq.com/show.asp?id=2195 will someone help me understand: 1. with *SQL Server*, do i keep my current Access 2000 database and ASP pages?
2
12653
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 ${SCHEMA_NM}/${SCHEMA_PASSWD}@${DB_NM} <<-ANALYZE_TABLE SET TERMOUT ON
0
10183
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 database table, using dynamic sql. Executing 'EXEC SQL DESCRIBE SELECT LIST FOR S INTO select_dp;' the error code -2149 is returned That means "Specified partition does not exist". Does anybody know if it is a database problem or a problem of
2
15216
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 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. In fact so many of you kind people confess to having succeeded in doing it. I have tried...
11
4250
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 threads of a multithreaded program using embedded SQL. Since the threads do not need to share transaction scopes, the sqleAttachToCtx family of APIs do not seem to be necessary. <quote> In the default implementation of threaded applications against...
4
7295
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 get the same error message: "An error occurred while creating one or more registry entries. Please see C:\WINDOWS\sqlstp.log for details. The problem could be caused by a low registry quota condition" I have tried to clean the registry and i...
1
6612
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 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an unexpected value. ***EndOfSession***? Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an...
6
2489
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 stored in the .py files and run in the .cgi files. I have the problem that I need to construct a row from two seprate SQL Querys, I have tried combining the two Querys but all that does is create a Query that returns nothing after a long period...
14
3018
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 anyone please help me with this as I want to install SQL server and also wouold be grateful, if you can suggest me any workaround to dealwith this problem.(Like should I install any new OS etc). Any help would be appreciated.
5
9274
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 records from an external Oracle source using a SQL statement. The SQL statement works as expected when the loop code is commented out, but I receive an error "SQL command not properly ended" when the loop is active. Do Until intLoop = UserNum ...
0
8596
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8561
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8240
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8411
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7038
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.