472,347 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,347 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 11684

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...
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...
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...
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...
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 ) -...
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...
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...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.