0

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

但是当我运行合并语句时,它显示一个错误:

ora-04091 - Table1 正在变异,触发器/函数可能看不到它

我怎样才能解决这个问题?

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;
4

2 回答 2

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;
于 2013-10-03T05:52:27.770 回答
0

您可以考虑使用临时表 IE:

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;

这相当“快速而肮脏”,但可以作为一个开始

于 2013-10-03T05:32:02.417 回答