http://community.csdn.net/Expert/topic/3313/3313991.xml?temp=.6611292

解决方案 »

  1.   

    轉自OTN網,beckham發表
    返回记录集过程:
    CREATE OR REPLACE PACKAGE pkg_test
    AS
      TYPE myrctype IS REF CURSOR;  PROCEDURE get (p_id NUMBER, p_rc OUT myrctype);
    END pkg_test;
    /CREATE OR REPLACE PACKAGE BODY pkg_test
    AS
      PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
      IS
         sqlstr   VARCHAR2 (500);
      BEGIN
         IF p_id = 0 THEN
            OPEN p_rc FOR
               SELECT ID, NAME, sex, address, postcode, birthday
                 FROM student;
         ELSE
            sqlstr :=
               'select id,name,sex,address,postcode,birthday
              from student where id=:w_id';
            OPEN p_rc FOR sqlstr USING p_id;
         END IF;
      END get;
    END pkg_test;
    /
    --------------------------------------------------------------------------------------------------------------
    返回记录集函数:
    1、建立测试表
    CREATE TABLE student
    (
     id                         NUMBER,
     name                       VARCHAR2(30),
     sex                        VARCHAR2(10),
     address                    VARCHAR2(100),
     postcode                   VARCHAR2(10),
     birthday                   DATE,
     photo                      LONG RAW
    );
    /2、建立带ref cursor定义的包和包体及函数:
    CREATE OR REPLACE
    package pkg_test as
    /* 定义ref cursor类型
      不加return类型,为弱类型,允许动态sql查询,
      否则为强类型,无法使用动态sql查询;
    */
     type myrctype is ref cursor; --函数申明
     function get(intID number) return myrctype;
    end pkg_test;
    /CREATE OR REPLACE
    package body pkg_test as
    --函数体
      function get(intID number) return myrctype is
        rc myrctype;  --定义ref cursor变量
        sqlstr varchar2(500);
      begin
        if intID=0 then
           --静态测试,直接用select语句直接返回结果
           open rc for select id,name,sex,address,postcode,birthday from student;
        else
           --动态sql赋值,用:w_id来申明该变量从外部获得
           sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
           --动态测试,用sqlstr字符串返回结果,用using关键词传递参数
           open rc for sqlstr using intid;
        end if;    return rc;
      end get;end pkg_test;
    /3、用pl/sql块进行测试:
    declare
     w_rc       pkg_test.myrctype; --定义ref cursor型变量 --定义临时变量,用于显示结果
     w_id       student.id%type;
     w_name     student.name%type;
     w_sex      student.sex%type;
     w_address  student.address%type;
     w_postcode student.postcode%type;
     w_birthday student.birthday%type;begin
     --调用函数,获得记录集
     w_rc := pkg_test.get(1); --fetch结果并显示
    loop
    fetch w_rc into w_id,w_name,w_sex,w_address,w_postcode,w_birthday;
    exit when w_rc%notfound;
    dbms_output.put_line(w_name);
    end loop;
    end;4、测试结果:
    通过。
    --------------------------------------------------------------------------------------------------------------
    返回对象类型:
    SQL> create table a (id number,name varchar2(50),doctime date);Table created.--插入六条测试数据:
    SQL> insert into a values (1,'aaa',to_date('2002-07-01','yyyy-mm-dd'));1 row created.SQL> insert into a values (2,'bbb',to_date('2002-07-02','yyyy-mm-dd'));1 row created.SQL> insert into a values (3,'ccc',to_date('2002-07-03','yyyy-mm-dd'));1 row created.SQL> insert into a values (4,'ddd',to_date('2002-07-04','yyyy-mm-dd'));1 row created.SQL> insert into a values (5,'eee',to_date('2002-07-05','yyyy-mm-dd'));1 row created.SQL> insert into a values (6,'fff',to_date('2002-07-06','yyyy-mm-dd'));1 row created.SQL> commit;Commit complete.--创建两个type
    SQL> create or replace type myobjectype as object (x int,y date,z varchar2(50));
     2  /Type created.SQL> create or replace type mytabletype as table of myobjectype
     2  /Type created.
      

  2.   

    谢谢楼上的兄弟。
    其实我就是要练习使用FOR语句
    如果我要在上面的语句中一定要用FOR语句应该如何做呢?
      

  3.   

    -------------你要的
    for c in (select.............)
    loop
      
    end loop;
    --------------
    loop
      
      exit when i;
    end loop;
    -------------
    i := tttt.first;
    while i is not null 
    loop
        
      i := tttt.next(i);
    end loop;
    -------------
    open cur_1;
    loop
      fetch cur_1 into recVar;
      exit when cur_1%notfound;
      
    end loop;
    close cur_1;-------------
    for i in 1..10
    loop
      
    end loop;
    -------------------
    while true
    loop
      
    end loop;
      

  4.   

    for c in (select userid from tbl_userinfo)
    loop
      dbms_output.put_line(c.userid);
    end loop;
      

  5.   

    --open c_cursor for  select USERNAME,USERPWD FROM TEST6.TEST_2;--查询
           FOR V_Record_Userlist in (select USERNAME,USERPWD FROM TEST6.TEST_2) loop---使用游标,这句话出错!!!
            DBMS_OUTPUT.PUT_LINE(V_Record_Userlist.USERNAME||V_Record_Userlist.USERPWD);--打印
           END LOOP;