使用动态sql删除重复记录的过程

时间:2013-07-30 07:14:02

标签: sql plsql dynamic-sql

我已经成功实现了一个可以从表中删除删除重复记录的过程。 继续我正在进一步尝试使用Dynamic sql来概括这个过程但是在编译时我得到以下错误。

ERROR line 13, col 7, ending_line 14, ending_col 14, Found ''SELECT ROWID, ERR_MOD FROM '', Expecting: ( SELECT -or- WITH

这是我的代码: -


CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
   p_tblname   IN     VARCHAR2,
   p_cname   IN varchar2,
   p_cvalue  IN varchar2,
   p_out          OUT VARCHAR2:=0
)
IS
   v_cnt   NUMBER;

CURSOR TMP IS 'SELECT ROWID, ERR_MOD FROM ' || p_tblname|| 'WHERE '|| p_cname '='||''p_cvalue ''; BEGIN BEGIN 'SELECT COUNT ( * ) INTO v_cnt FROM ' || p_tblname|| 'WHERE '|| p_cname' = '||''p_cvalue''; EXCEPTION WHEN OTHERS THEN p_out:=1; end;

FOR REC IN tmp LOOP IF v_cnt = 1 THEN EXIT; ELSE 'DELETE FROM '|| p_tblname|| 'WHERE ROWID = REC.ROWID'; END IF;

  v_cnt := v_cnt - 1;

END LOOP; EXCEPTION WHEN OTHERS THEN p_out := 2; END; /

1 个答案:

答案 0 :(得分:1)

您好,您无法为正常游标创建动态查询,在这种情况下您应该使用ref游标并按照以下步骤操作 - 创建查询字符串 - 打开引用光标 请使用执行立即执行删除语句...

CREATE OR REPLACE PROCEDURE BIOCON.proc_dedup (
   p_tblname   IN     VARCHAR2,
   p_cname   IN varchar2,
   p_cvalue  IN varchar2,
   p_out          OUT VARCHAR2:=0

 IS
   v_cnt   NUMBER;
   v_string varchar2(200);
   TYPE ref_cur IS REF CURSOR
   ref_cursor_name ref_cur;

BEGIN
      v_string:='SELECT ROWID, ERR_MOD FROM ' || p_tblname|| 'WHERE '|| p_cname    
      '='||''p_cvalue ''; 
  OPEN cursor_variable_name FOR v_string;
   .....
     .....


  EXECUTE IMMEDIATE 'DELETE FROM '|| p_tblname|| 'WHERE ROWID = REC.ROWID';--you need execute immediate for this

..... .....