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

PL/SQL-CURSOR - 5

debasisdas
8,127 Expert 4TB
SAMPLE CODE USING RECORD
===========================
Expand|Select|Wrap|Line Numbers
  1. declare
  2. cursor c1 is select * from dept;
  3. type drec is record (a dept.deptno%type,
  4.   b dept.dname%type,
  5.   c dept.loc%type);
  6. type ttype is table of drec index by binary_integer;
  7. dr drec;
  8. tt ttype;
  9. i binary_integer:=1;
  10. cnt number;
  11. begin
  12. open c1;
  13. fetch c1 into tt(i);
  14. while (c1%found=TRUE) loop
  15. dbms_output.put_line(tt(i).a||' '||tt(i).b||'   '||tt(i).c);
  16. i := i+1;
  17. fetch c1 into tt(i);
  18. end loop;
  19. cnt := c1%rowcount;
  20. close c1;
  21. end;
  22.  
TAB CURSOR-1
----------------------
Expand|Select|Wrap|Line Numbers
  1.  declare
  2.  type tabtype is table of emp%rowtype index by binary_integer;
  3.  rec tabtype;
  4.  cursor c1 is select * from emp;
  5.  counter number:=1;
  6.  begin
  7.  open c1;
  8.  loop
  9.  fetch c1 into rec(counter);
  10.  exit when c1%notfound;
  11.  counter:=counter+1;
  12.  end loop;
  13. close c1;
  14. for i in rec.first..rec.last
  15. loop
  16. dbms_output.put_line(rec(i).empno||' '||rec(i).ename||' '||rec(i).sal);
  17. end loop;
  18. dbms_output.put_line('total record fetched = ' ||rec.count);
  19. end;
  20.  
TAB CURSOR--4
---------------------------
Expand|Select|Wrap|Line Numbers
  1. declare
  2. type my_table_type is table of emp.ename%type
  3. index by binary_integer;
  4. my_table my_table_type;
  5. begin
  6. for i in 1..5 loop
  7.     select ename
  8.     into my_table(i)
  9.     from emp
  10.     where empno=&empno;
  11. end loop;
  12. for i in 1..5 loop
  13.     dbms_output.put_line(my_table(i));
  14. end loop;
  15. end ;
  16.  
TAB--CURSOR-3
--------------------------
Expand|Select|Wrap|Line Numbers
  1. declare
  2. type tabtype is table of emp%rowtype index by binary_integer;
  3. rec tabtype;
  4. type tabtype1 is table of dept%rowtype index by binary_integer;
  5. rec1 tabtype1;
  6. cursor c1(p number) is select * from emp where deptno=p;
  7. cursor c2 is select * from dept;
  8. counter number:=1;
  9. counter1 number:=1;
  10. begin
  11. open c2;
  12. loop
  13. fetch c2 into rec1(counter1);
  14. exit when c2%notfound;
  15. dbms_output.put_line(rec1(counter1).deptno||' '||rec1(counter1).dname||' '||rec1(counter1).loc);
  16. open c1(rec1(counter1).deptno);
  17. loop
  18. fetch c1 into rec(counter);
  19. exit when c1%notfound;
  20. dbms_output.put_line(rec(counter).empno||' '||rec(counter).ename||' '||rec(counter).sal);
  21. counter:=counter+1;
  22. end loop;
  23. dbms_output.put_line('total record fetched = ' ||rec.count);
  24. counter1:=counter1+1;
  25. close c1;
  26. end loop;
  27. close c2;
  28. end;
  29.  
TAB CURSOR--4
---------------------------
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.    TYPE t_type IS TABLE
  3.                   OF emp.empno%TYPE INDEX BY BINARY_INTEGER;
  4.    len BINARY_INTEGER := 0;
  5.    tab t_type;
  6.    CURSOR c1 IS SELECT empno FROM emp ORDER BY empno;
  7. BEGIN
  8.    OPEN c1;
  9.    LOOP
  10.       FETCH c1 INTO tab(len+1);
  11.       EXIT WHEN c1%NOTFOUND;
  12.       len := len + 1;
  13.       EXIT WHEN (c1%ROWCOUNT = 10);
  14.    END LOOP;
  15.    CLOSE c1;
  16. END;
  17.  
SAMPLE EXAMPLE OF PARAMETARISED CURSOR
==========================================
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.  ob_type   VARCHAR2(6);
  3.  ob_schema VARCHAR2(32);
  4.  ob_name   VARCHAR2(32);
  5.  ob_stat   VARCHAR2(32);
  6.  cursor chk_it (ob_name VARCHAR2) IS SELECT status FROM dba_objects WHERE object_name=ob_name;
  7. BEGIN
  8.   ob_type:='PACKAGE';
  9.  ob_schema:='ACCT_DBA';
  10.  ob_name:= 'CHARGE_FEES';
  11.  IF chk_it%ISOPEN THEN  CLOSE chk_it;
  12.   OPEN chk_it(ob_name);
  13.  ELSE
  14.   OPEN chk_it(ob_name);
  15.  END IF;
  16.  FETCH chk_it INTO ob_status;
  17.  IF ob_status = 'INVALID' THEN
  18.  DBMS_DDL.ALTER_COMPILE(ob_type,ob_schema,ob_name);
  19.  END IF;
  20.  END;
  21. /
  22.  
May 29 '07 #1
0 11767

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.