日期:2014-05-17  浏览次数:20776 次

帮忙看一下这个触发器,谢谢
在sql   server里是正确的,即插入或更新表TTG_Account时,也要对TTG_Customer   进行更新,改称oracle触发器后,更新TTG_Account时更新了TTG_Customer,这是正确的,但是插入TTG_Account时,并没有插入TTG_Customer

这个触发器怎么调试阿?

Create   Trigger   TRG_TTG_Account_Insert_Customer
ON   TTG_Account
After   Insert,Update
As
Declare   @CustomerNoNew   varchar(6),@CustomerNoOld   varchar(6)

If   Update   (AccountID)
Begin
      select   @CustomerNoOld=AccountID   from   deleted
      select   @CustomerNoNew=AccountID   from   inserted

      If   Not   Exists   (select   CustomerID   from   TTG_Customer   where   CustomerNO=@CustomerNoOld)
Begin
Insert   into   TTG_Customer   (CustomerNO)   Values   (@CustomerNoNew)
End
      Else
Begin
Update   TTG_Customer   Set   CustomerNO=@CustomerNoNew   where   CustomerNO=@CustomerNoOld

      End  

End

-------------------------------
一下是oracle触发器

Create   Or   Replace   Trigger   TR_TTG_Account_Insert_Customer
After   Insert   or   Update
ON   TTG_Account
FOR   EACH   ROW
Declare
v_CustomerNoNew   varchar2(6);
v_CustomerNoOld   varchar2(6);
v_CustomerID   number(10,0);

Begin

        v_CustomerNoOld:=:OLD.AccountID;
        v_CustomerNoNew:=:NEW.AccountID;
        IF   (v_CustomerNoOld <> v_CustomerNoNEW)   THEN
              select   CustomerID   into   v_CustomerID   from   TTG_Customer   where   CustomerNO=:OLD.AccountID;
        IF     v_CustomerID   is   null   THEN
                Insert   into   TTG_Customer   (CustomerNO)   Values   (:NEW.AccountID);
        ELSE
                UPDATE   TTG_Customer   Set   CustomerNO=:NEW.AccountID   where   CustomerNO=:OLD.AccountID;
        END   IF;
        END   IF;
End   TR_TTG_Account_Insert_Customer;


------解决方案--------------------
语法不存在错误,但是判断是插入还是update,请使用
IF INSERTING
IF UPDATING
来处理.
------解决方案--------------------
就是楼上说的判断。