473,387 Members | 1,282 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,387 developers and data experts.

PL/SQL-CURSOR - 4

debasisdas
8,127 Expert 4TB
This thread contains some useful tips/samples regarding some advance concepts in cursors.

FEW MORE EXAMPLES
===================
Expand|Select|Wrap|Line Numbers
  1. declare
  2. er emp%rowtype;
  3. cursor c1 is select * from emp;
  4. begin
  5. open c1;
  6. loop
  7. fetch c1 into er;
  8. exit when c1%notfound;
  9. if er.job='SALESMAN' then
  10. dbms_output.put_line(er.empno||' '||er.ename||'  '||er.sal||'  '||er.sal*1.10);
  11.  
  12. elsif er.job='CLERK' then
  13. dbms_output.put_line(er.empno||' '||er.ename||'  '||er.sal||'  '||er.sal*1.08);
  14.  
  15. else
  16. dbms_output.put_line(er.empno||' '||er.ename||'  '||er.sal||'  '||'No ince req.');
  17. end if;
  18.  
  19. end loop;
  20. close c1;
  21. end;
  22.  
NOTE:--PLEASE TRY THE ABOVE CODE IN SCOTT SCHEMA

EXAMPLE #1
--------------------
Expand|Select|Wrap|Line Numbers
  1. begin
  2. --no need to open and close.
  3. for dr in (select * from dept) loop
  4. dbms_output.put_line(dr.deptno||'  '||dr.dname||'  '||dr.loc);
  5. end loop;
  6. end;
  7.  
EXAMPLE #2
----------------------
Expand|Select|Wrap|Line Numbers
  1. declare
  2. --cursor is declared
  3. cursor c1 is select * from dept;
  4. -- a cur type variable is declared
  5. dr c1%rowtype;
  6. begin
  7. -- open the cursor
  8. open c1;
  9. --fetch into the target variable
  10. fetch c1 into dr;
  11. -- check for existance of more dat in the cursor
  12. while (c1%found=TRUE) loop
  13. fetch c1 into dr;
  14. dbms_output.put_line(dr.deptno||' '||dr.dname||'   '||dr.loc);
  15. --end the loop
  16. end loop;
  17. --close the cursor.
  18. close c1;
  19. end;
  20.  

SAMPLE PROGRAM TO SHOW USE OF RECORD TYPE IN CURSOR
-----------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. declare
  2. TYPE MYTYPE IS RECORD
  3. (
  4. MENAME VARCHAR2(10),
  5. MMGR NUMBER(4)
  6. );
  7. mm mytype;
  8. CURSOR C1 IS
  9. select ename,mgr from emp where deptno=20 and job='CLERK';
  10. begin
  11. open C1;
  12. LOOP
  13. FETCH C1 INTO mm.mename,mm.mmgr;
  14. EXIT WHEN C1%NOTFOUND;
  15. DBMS_OUTPUT.PUT_LINE(mm.mename || ' '|| mm.mmgr);
  16. END LOOP;
  17. CLOSE C1;
  18. END;
  19.  

Also Check PL/SQL-CURSOR - 5
Sep 17 '07 #1
0 4646

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...
10
by: Dagwood | last post by:
Good morning: At least it's morning where I am. :) I have a rather newbie question I'm afraid. I have VisualStudio.NET, and have installed it along with SQL server. However I can't seem to...
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...
6
by: Andreas Lauffer | last post by:
I changed from Access97 to AccessXP and I have immense performance problems. Details: - Access XP MDB with Jet 4.0 ( no ADP-Project ) - Linked Tables to SQL-Server 2000 over ODBC I used...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
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...
0
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...

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.