0
create or replace
PROCEDURE PROMOLOG1 AS
CURSOR rule IS SELECT * FROM business_rules; --this table had where conditions to use
TYPE r_cursor IS REF CURSOR;
ruleMatch r_cursor;
row_id VARCHAR2(20);
query_test VARCHAR2(400);

BEGIN
  FOR a IN rule LOOP
    IF a.rule_name != 'Rule1' THEN
      OPEN rulematch FOR 'SELECT rowid FROM cold_promo_log_dup P WHERE EXISTS( SELECT   1 FROM cold_promo_log_dup C WHERE ' || a.condition || ' AND rowid < P.rowid)';
      dbms_output.put_line('matching completed for ' || a.rule_name);
      LOOP
         FETCH ruleMatch INTO row_id;
         EXIT WHEN rulematch%notfound;
         query_test := 'INSERT INTO cold_promo_log_duplicate SELECT * FROM cold_promo_log_dup WHERE rowid = ''' || row_id ||'''' ;
         dbms_output.put_line(query_test);
         --EXECUTE IMMEDIATE 'INSERT INTO cold_promo_log_duplicate SELECT * FROM cold_promo_log_dup WHERE rowid = ''' || row_id || '''';
         INSERT INTO cold_promo_log_duplicate SELECT * FROM cold_promo_log_dup WHERE rowid = row_id ;
         EXECUTE IMMEDIATE 'DELETE FROM cold_promo_log_dup WHERE rowid = ' || row_id;
      END LOOP;
    END IF;
  END LOOP;
END PROMOLOG1;

在oracle中执行上述过程会引发以下错误:

    Connecting to the database dedupe_preprod.
    ORA-00904: "AAALJBABVAANSZDAAA": invalid identifier
    ORA-06512: at "DEDUPE.PROMOLOG1", line 23
    ORA-06512: at line 2
    matching completed for Rule2
    INSERT INTO cold_promo_log_duplicate SELECT * FROM cold_promo_log_dup WHERE rowid =   'AAALJBABvAANsZDAAA'
    Process exited.
    Disconnecting from the database dedupe_preprod.

当我打印 rowid 时,它有几个小写字母(AAALJBABvAANsZDAAA),但错误显示所有字母都大写(AAALJBABVAANSZDAAA)。数据库中没有大写的rowid,因此出现错误无效标识符。
任何帮助表示赞赏。

4

2 回答 2

3

首先你的row_id 变量应该是ROWID 类型而不是varchar2。

其次这是错误:

 EXECUTE IMMEDIATE 'DELETE FROM cold_promo_log_dup WHERE rowid = ' || row_id;

您没有引用它(因此它将 rowid 视为标识符)。但是你为什么要在动态 SQL 中运行它呢?

Ps 如果你要离开这个动态,你应该使用绑定变量,因此不要破坏你的共享池并提高性能。

于 2013-01-23T18:07:21.353 回答
0

不要使用 rowid 进行循环。您这样做:您可以删除该行,然后将其插入其他位置。最好的办法是使用 a DELETE ... RETURNING BULK COLLECT ...,这样你就不需要使用 rowid,你可以使用集合而不是使用将逐行执行的 for 循环

于 2013-01-23T19:34:25.497 回答