ORACLE将自定义tableObject用于函数中

时间:2015-12-21 17:03:06

标签: oracle

我想将光标插入我的自定义tableObject,但并不总是找到它。

我的记录:

create or replace type "RECORDRANKING" as object
(

  -- Attributes
  COL1 NUMBER,
  COL2 VARCHAR(50),
  COL3 NUMBER

  -- Member functions and procedures
 -- member procedure <ProcedureName>(<Parameter> <Datatype>)
)

这是对象表:

CREATE OR REPLACE TYPE "TBRANKING" AS TABLE OF RECORDRANKING;

现在我开始创建一个函数(进入包):

CREATE OR REPLACE PACKAGE BODY PKLBOTTONI as
FUNCTION testlb
( 
p_gapup IN NUMBER, 
p_gadown IN NUMBER
)
RETURN SYS_REFCURSOR IS
cursor_ranking SYS_REFCURSOR;
position NUMBER ;
gap_ranking TBRANKING;
gaprecord RECORDRANKING;
upgap NUMBER;
downgap NUMBER;
     BEGIN
      select * into cursor_ranking from(
        select pkranking.getRanking(  100 ) from dual); 

LOOP  
FETCH cursor_ranking INTO gap_ranking;
EXIT WHEN cursor_ranking%NOTFOUND;
   INSERT INTO gap_ranking       (COL1,COL2,COL3)
   VALUES 
   (cursor_ranking.C1,
   cursor_ranking.C2,
   cursor_ranking.C3);
 END LOOP;
return gap_ranking; 
END;


END PKLBOTTONI;

我总是得到:

Compilation errors for PACKAGE BODY PKLBOTTONI

Error: PL/SQL: ORA-00942: table or view does not exist
Line: 32
Text: INSERT INTO gap_ranking

1 个答案:

答案 0 :(得分:0)

在循环中,您将同时进入&#34; gap_ranking&#34;然后尝试插入&#34;它再次。插入集合不是有效的语法。可以在循环中使用Fetch,也可以使用批量收集一次获取多个记录。

从您的摘录中看,您看起来没有物理数据库表,因此在PL / SQL中执行此操作的方式如下所示。

有关使用集合的更多帮助,您也可以查看下面的Oracle文档: http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/collections.htm

SQL> set serveroutput on
SQL> declare
  2    gap_ranking tbranking := tbranking(null); -- initialize
  3  begin
  4    gap_ranking.delete; -- clear empty record
  5    for cur in
  6      (select level as i from dual connect by level <= 5)
  7    loop
  8      -- insert empty
  9      gap_ranking.extend;
 10      -- attribute values
 11      gap_ranking(gap_ranking.last) := recordranking(1000 + cur.i, 'TEST' || cur.i, 10 + cur.i);
 12    end loop;
 13    -- loop to print - just to illustrate
 14    for j in gap_ranking.first .. gap_ranking.last
 15    loop
 16      dbms_output.put_line(gap_ranking(j).col1 || ',' ||
 17                           gap_ranking(j).col2 || ',' ||
 18                           gap_ranking(j).col3);
 19  
 20    end loop;
 21    -- same as...
 22    for j in 1 .. gap_ranking.count
 23    loop
 24      dbms_output.put_line(gap_ranking(j).col1 || ',' ||
 25                           gap_ranking(j).col2 || ',' ||
 26                           gap_ranking(j).col3);
 27  
 28    end loop;
 29  end;
 30  /
1001,TEST1,11
1002,TEST2,12
1003,TEST3,13
1004,TEST4,14
1005,TEST5,15
1001,TEST1,11
1002,TEST2,12
1003,TEST3,13
1004,TEST4,14
1005,TEST5,15
PL/SQL procedure successfully completed

SQL>