oracle 如何将游标值提取到对象中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31742447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:58:10  来源:igfitidea点击:

how to fetch cursor values into an object

oracleplsqluser-defined-typesobject-type

提问by manikandan

I want to fetch values from a cursor and store them in an object.... I tried doing the same with Record i got the output

我想从游标中获取值并将它们存储在一个对象中......我尝试对 Record 做同样的事情,我得到了输出

DECLARE
CURSOR lc_emp_fetch 
IS 
  SELECT emp_no,emp_name FROM maniemp;
  TYPE r_emp_record IS RECORD (
                               eno maniemp.emp_no%TYPE,
                               ename maniemp.emp_name%TYPE
                              );
TYPE t_emp IS TABLE OF r_emp_record;
lt_emp_rcd t_emp;                            
BEGIN
  OPEN lc_emp_fetch;
  LOOP
  FETCH lc_emp_fetch BULK COLLECT INTO lt_emp_rcd LIMIT 5;
  EXIT WHEN lt_emp_rcd.COUNT=0;
    FOR indx IN 1..lt_emp_rcd.COUNT
    LOOP
      DBMS_OUTPUT.PUT_LINE(lt_emp_rcd(indx).eno||lt_emp_rcd(indx).ename);
    END LOOP;
END LOOP;
CLOSE lc_emp_fetch;
END;
/                           

but when i try doing the same in an object its not working... i surfed all the websites but didn't get proper example program. This is my object:

但是当我尝试在一个对象中做同样的事情时它不起作用......我浏览了所有网站,但没有得到正确的示例程序。这是我的对象:

CREATE OR REPLACE TYPE Typename3 AS OBJECT ( 
  eno number, 
  ename varchar2(500), 
  esal number);

SHOW ERRORS;        

I am new to this i don't know how to do this can someone help me with this

我是新手,我不知道该怎么做,有人可以帮我解决这个问题吗

回答by Tharunkumar Reddy

If you want to try the above example with an object and type then you should create both are at schema level it means

如果你想用一个对象和类型来尝试上面的例子,那么你应该在模式级别创建它们,这意味着

 CREATE OR REPLACE type R_EMP_OBJECT as object(
                                   eno number,
                                   ename varchar2(30)
                                  );

and

 `create or replace type t_emp IS TABLE OF r_emp_object`;

then

然后

DECLARE    
        lt_emp_rcd t_emp;                            
        BEGIN
          select r_emp_object (emp,ename) bulk collect into lt_emp_rcd 
     FROM emp;
            FOR indx IN 1..lt_emp_rcd.COUNT
            LOOP
              DBMS_OUTPUT.PUT_LINE(lt_emp_rcd(indx).eno||lt_emp_rcd(indx).ename);
            END LOOP;  
        END;

EditI have tried with cursors, the below code is working fine

编辑我用游标试过,下面的代码工作正常

 DECLARE 
 CURSOR C1
 IS   
  SELECT emp_no,emp_name FROM maniemp;
 C2 C1%ROWTYPE;
 LT_EMP_RCD T_EMP;                            
 BEGIN
 OPEN C1;
 LOOP
 FETCH  C1 INTO C2 ;
 SELECT R_EMP_OBJECT(C2.EMP_NO,C2.EMP_NAME) BULK COLLECT INTO LT_EMP_RCD FROM DUAL;
 EXIT WHEN C1%NOTFOUND;
 FOR INDX IN 1..LT_EMP_RCD.COUNT
 LOOP
 DBMS_OUTPUT.PUT_LINE(LT_EMP_RCD(INDX).ENO||' '||LT_EMP_RCD(INDX).ENAME);
 END LOOP;
 END LOOP; 
 CLOSE C1; 
 END;