PLS-00222:没有名称' LV_RATING_AGENCY_TAB'存在于此范围内

时间:2017-10-12 21:51:32

标签: sql oracle plsql

我还在学校学习PL / SQL,所以要善待。 我正在尝试创建一个范围for循环,该循环读取集合内容并通过使用集合的复合对象类型的成员检查ITEM_RATING和ITEM_RATING_AGENCY列值来更新项目表中的RATING_AGENCY_ID列。

这是我的代码:

DECLARE
    CURSOR c IS
        SELECT a.rating_agency_id AS id
             , a.rating AS rating
             , a.rating_agency AS agency
        FROM   rating_agency_table a;

    -- Create a collection of rating_agency
    lv_rating_agency_tab rating_agency;
BEGIN
    FOR i IN c LOOP
        lv_rating_agency_tab := rating_agency( rating_agency_id => i.id
                                             , rating => i.rating
                                             , rating_agency => i.agency );
    END LOOP;

    FOR i IN 1..lv_rating_agency_tab(i).COUNT LOOP
        INSERT INTO item
        ( rating_agency_id
        , item_rating
        , item_rating_agency )
        VALUES
        ( lv_rating_agency_tab.rating_agency_id
        , lv_rating_agency_tab.rating
        , lv_rating_agency_tab.rating_agency );
    END LOOP;

我无法弄清楚我做错了什么,但我收到以下错误:

FOR i IN 1..lv_rating_agency_tab(i).COUNT LOOP
            *
ERROR at line 18:
ORA-06550: line 18, column 13:
PLS-00222: no function with name 'LV_RATING_AGENCY_TAB' exists in this scope
ORA-06550: line 18, column 1:
PL/SQL: Statement ignored

2 个答案:

答案 0 :(得分:0)

我正在纠正你的代码..让你试试..因为我不知道你的桌子和...类型结构,所以你可以尝试下面的代码..如果有任何困难请告诉我。

代码:

DECLARE
    CURSOR c IS
        SELECT a.rating_agency_id AS id
             , a.rating AS rating
             , a.rating_agency AS agency
        FROM   rating_agency_table a;

    -- Create a collection of rating_agency
    lv_rating_agency_tab rating_agency;
BEGIN
    FOR i IN c LOOP
        lv_rating_agency_tab := rating_agency( rating_agency_id => i.id
                                             , rating => i.rating
                                             , rating_agency => i.agency );
    END LOOP;

    FOR i IN 1..lv_rating_agency_tab.COUNT 
    LOOP
        INSERT INTO item
        ( rating_agency_id
        , item_rating
        , item_rating_agency )
        VALUES
        ( lv_rating_agency_tab(i).rating_agency_id
        , lv_rating_agency_tab(i).rating
        , lv_rating_agency_tab(i).rating_agency );
    END LOOP;

    end;

答案 1 :(得分:0)

您可以按照以下方式执行此操作:

表&类型:

CREATE TYPE rating_agency AS OBJECT
(
   rating_agency_id NUMBER,
   rating NUMBER,
   rating_agency VARCHAR2 (100)
);
/
create table rating_agency_table
(
   rating_agency_id NUMBER,
   rating NUMBER,
   rating_agency VARCHAR2 (100)
);
/
create  table item 
(
rating_agency_id NUMBER, 
item_rating NUMBER, 
item_rating_agency  VARCHAR2 (100)
)

块:

DECLARE
   CURSOR c
   IS
      SELECT rating_agency (a.rating_agency_id, a.rating, a.rating_agency)
        FROM rating_agency_table a;

   -- Create a collection of rating_agency
   TYPE typ_rating_agency IS TABLE OF rating_agency
      INDEX BY PLS_INTEGER;

   lv_rating_agency_tab   typ_rating_agency;
BEGIN
   OPEN c;

   FETCH c BULK COLLECT INTO lv_rating_agency_tab;

   CLOSE c;

   FOR i IN 1 .. lv_rating_agency_tab.COUNT
   LOOP
      INSERT INTO item (rating_agency_id, item_rating, item_rating_agency)
              VALUES (
                        lv_rating_agency_tab (i).rating_agency_id,
                        lv_rating_agency_tab (i).rating,
                        lv_rating_agency_tab (i).rating_agency);
   END LOOP;
   COMMIT;
END;

输出:

SQL> SELECT * FROM ITEM;

RATING_AGENCY_ID ITEM_RATING ITEM_RATING_AGENCY
---------------- ----------- ---------------------------------------------------------------------------------------------
             111           1 FFFF
             222           2 XXX