ora-04091表是Mutating-

时间:2013-10-03 04:18:04

标签: oracle ora-04091

我正在使用一个比较表1和表2中所有列的函数,并返回'Y'或'N'。基于此,我将更新我的表1。

但是当我运行merge语句时,它会显示错误:

  

ora-04091 - 表1发生变异,触发/功能可能看不到它

我该如何解决这个问题?

CREATE OR REPLACE function  DataChange  (in_epmname varchar2) return char  is
    v_epmname   table2.empname%type;
    v_DATA_COUNT  varchar2(2);
    v_DATA_CHANGED  char;
begin
    SELECT COUNT (*) into v_DATA_COUNT
    FROM
    (
        SELECT trim(column1||column2||column3)
        FROM table1
        WHERE empname  =  in_epmname
        UNION
        SELECT trim(column1||column2||column3)
        FROM table2
        WHERE empname = in_epmname
    );
    If (v_DATA_COUNT = '1' ) Then
        v_DATA_CHANGED :='N';
    else
        v_DATA_CHANGED :='Y';
    end if;
    return v_DATA_CHANGED;
end DataChange  ;

我正在使用的合并声明是:

CREATE OR REPLACE PROCEDURE updatetabble1 AS
BEGIN
    MERGE
        INTO Table1 DBC
        USING  ( SELECT
            empname,
            DataChange(empname) as DATA_CHANGED
            FROM employee
        ) TBL_MAIN
        ON ( DBC.empname = TBL_MAIN.empname    )
        WHEN MATCHED THEN
          UPDATE SET
          DBC.DATA_CHANGED = TBL_MAIN.DATA_CHANGED;
    COMMIT;     
END updatetabble1;

2 个答案:

答案 0 :(得分:1)

Chade我试过这个选项它没有给我任何错误。

CREATE OR REPLACE PROCEDURE updatetabble1 AS
    BEGIN
        MERGE
        INTO Table1 DBC
        USING  (            Select ename from
            (
            Select ename
            from
                (
                Select ename, column1||Column2||Column3 from table1
                union
                Select ename, column1||Column2||Column3 from table2 
                ) 
            ) 
        GROUP BY ename HAVING count(*) > 1
        ) TBL_MAIN
                 ON ( DBC.empname = TBL_MAIN.empname)
                WHEN MATCHED THEN
 UPDATE SET DBC.DATA_CHANGED = 'Y';
COMMIT;     
END updatetabble1;

答案 1 :(得分:0)

您可以考虑使用临时表,I.E。:

create global temporary table table12_gt(empname varchar2(30), is_changed char(1));

CREATE OR REPLACE PROCEDURE updatetabble1_2 AS
  BEGIN
   insert into table12_gt
    select empname,
           CASE
             WHEN count(v) > 1 THEN
              'Y'
             ELSE
              'N'
           END is_changed
      from (SELECT empname, trim(column1 || column2 || column3) v
              FROM table1
            UNION
            SELECT empname, trim(column1 || column2 || column3) v FROM table2)
     GROUP BY empname;

  MERGE INTO Table1 DBC
  USING (SELECT empname, is_changed as DATA_CHANGED FROM table12_gt) TBL_MAIN
  ON (DBC.empname = TBL_MAIN.empname)
  WHEN MATCHED THEN
    UPDATE SET DBC.DATA_CHANGED = TBL_MAIN.DATA_CHANGED;

  COMMIT;
END updatetabble1_2;

这非常“快速而肮脏”,但可以是一个开始