SQL 更新触发器的使用

CREATE TRIGGER [dbo].[trig_so_salesOrder_update] ON [dbo].[so_salesOrder] AFTER update AS //定义一个变量 DECLARE @sfname varchar;

//判断是否有数据插入
  if @@rowcount = 0 
      return

 //判断插入的数据字段sfname是否为空,如果为空,不进行数据插入
   select @sfname = s.sfName from so_salesOrder s , inserted i where s.id = i.id if(@sfname = null or @sfname = '') return else //否则把数据插入到 erp_update_info 表 insert into erp_update_info(tableName, columnName, updateId, updateType, readState, execTime) select 'so_salesOrder','id' , id, 'update', '0', {fn NOW()} from inserted

插入
**

create TRIGGER [dbo].[trig_stock_warehouseorderitem_insert] ON [dbo].[stock_warehouseorderitem] AFTER insert AS DECLARE @id int;

 select @id = id from stock_warehouseorderitem where warehouseOrder_id in (select id from stock_warehouseOrder where istate = 4) if @@rowcount = 0 return insert into erp_update_info(tableName,columnName, updateId, updateType, readState, execTime) select 'stock_warehouseorderitem','id', @id, 'insert', '0', {fn NOW()} from inserted**

更新

ALTER TRIGGER [dbo].[trig_stock_warehouseorderitem_insert] ON [dbo].[stock_warehouseorderitem] AFTER insert AS DECLARE @id int select @id = id from inserted where id in (select id from stock_warehouseorderitem where warehouseOrder_id in (select id from stock_warehouseOrder where istate = 4 )) if @@rowcount = 0 return insert into erp_update_info(tableName,columnName, updateId, updateType, readState, execTime) select 'stock_warehouseorderitem','id',@id, 'insert', '0', {fn NOW()} from inserted

你可能感兴趣的:(sql,触发器)