Create TRIGGER trg_auto_c_note
ON sys_ypxx
FOR Insert
AS 
 if c_note is null then
 C_NOTE=Cast(I_ID as varchar)  
 end if
GO
表名为:sys_ypxx
在新增纪录时,如果C_NOTE字段为空,就取I_ID(自增的整数)字段的值?
请问要怎么写?

解决方案 »

  1.   

    Create TRIGGER trg_auto_c_note
    ON sys_ypxx
    FOR Insert
    AS  
    begin
     update sys_ypxx
     set C_NOTE=Cast(I_ID as varchar)  
     where c_note is null 
    end
    GO
      

  2.   

    Create TRIGGER trg_auto_c_note
    ON sys_ypxx
    FOR Insert
    AS  
    begin
     update sys_ypxx set C_NOTE=(select I_ID  from inserted)  
     where c_note is null 
    end
    GO
      

  3.   

    只处理当前插入记录
    Create TRIGGER trg_auto_c_note
    ON sys_ypxx
    FOR Insert
    AS  
    begin
     update s
     set s.C_NOTE=Cast(s.I_ID as varchar)  
     from sys_ypxx s,inserted i
     where s.i_id=i.i_id and i.c_note is null 
    end
    GO