Oracle触发器用于更新非规范化表

时间:2015-08-28 19:11:36

标签: sql oracle plsql

我有这些表格:

员工(EmployeeID,NameSurname,PhoneNumber,Email)

合同(ContractID,日期, EmployeeID ,NameSurname)

我想在UPDATE for Contract表上创建触发器:当我更新Contract表中的“EmployeeID”时,应该从Employee表中相应地更新NameSurname。另外,我触发了“TBU_Contract_Prohib”,它不允许直接更改Contract中的NameSurname字段。我写了这个:

CREATE OR REPLACE TRIGGER "TBU_Contract"
 BEFORE UPDATE OF "EmployeeID" ON "Contract"
 FOR EACH ROW 
DECLARE
v_namesurname VARCHAR(50);
pragma autonomous_transaction;
  BEGIN
   EXECUTE immediate 'alter trigger "TBU_Contract_Prohib" disable';
   COMMIT;

   SELECT "NameSurname"
   INTO v_namesurname
   FROM "Employee"
   WHERE "EmployeeID" = :NEW."EmployeeID";

   UPDATE "Contract"
   SET "NameSurname"        = v_namesurname
   WHERE "EmployeeID" = :NEW."EmployeeID";

   EXECUTE immediate 'alter trigger " TBU_Contract_Prohib" enable';
   COMMIT;
  END;

这编译没有错误。但是,当我测试它时,例如:

UPDATE "Contract"
SET
"EmployeeID" = 2
WHERE
"ContractID" = 2;

我收到错误:

SQL Error: ORA-00060: deadlock detected while waiting for resource
ORA-06512: at "SYSTEM.TBU_Contract", line 14
ORA-04088: error during execution of trigger 'SYSTEM.TBU_Contract'
00060. 00000 -  "deadlock detected while waiting for resource"
*Cause:    Transactions deadlocked one another while waiting for resources.
*Action:   Look at the trace file to see the transactions and resources
           involved. Retry if necessary.

有什么问题,怎么可能让这项工作成功?

1 个答案:

答案 0 :(得分:0)

也许以下内容对您更有效:

raco setup

祝你好运。