触发器

--创建insert插入类型触发器
if (object_id('tgr_product_insert', 'tr') is not null)
    drop trigger tgr_product_insert
go
create trigger tgr_product_insert
on products
    for insert --插入触发
as
        if exists(select * from inserted i where i.serial_number not in (select serial_number from wx_shop_product))      
         begin
    
 insert into  wx_shop_product ( [categoryId],[brandId],[sku] ,[productName] ,[erpName] ,[shortDesc],[unit_id]
      ,[unit] ,[weight] ,[description]
      ,[seo_title]  ,[seo_keywords] ,[seo_description]
      ,[recommended],[latest],[hotsale] ,[specialOffer]
      ,[costPrice] ,[marketPrice],[salePrice]
      ,[upselling]
      ,[stock]
      ,[importDate]
      ,[vistiCounts]
      ,[sort_id]
      ,[updateDate]
      ,[deleted]
      
      ,[integral]) 
           (select
         p.categoryid,'','',p.name,'',p.shortDesc,p.unit1_id,p.unit2_id,'',p.comment,'',
         '','',p.comment,'','','',0,0,0,'',p.standard,p.Inputdate,'',p.ifdiscount,p.Inputdate,p.deleted
         ,p.Integral
         
          from products p inner join  (select * from inserted ii where  ii.serial_number not in 
          (select serial_number from wx_shop_product)) p1  on p1.serial_number=p.serial_number)
       
         
         end
go

更新类触发器

--创建insert更新类型触发器
if (object_id('tgr_product_update', 'tr') is not null)
    drop trigger tgr_product_update
go
create trigger tgr_product_update
on products
    for update --更新触发
as
        if exists(select * from inserted i where i.serial_number not in (select serial_number from wx_shop_product))      
         begin
    
update   wx_shop_product
set 
  categoryId=p2.categoryid,brandId=p2.brandId,productName=p2.name,erpName=p2.erpName,
  shortDesc=p2.shortDesc,unit_id=p2.unit1_id,weight=p2.weight,description=p2.description,
  recommended=p2.recommended,latest=p2.isNew,hotsale=p2.isHot,specialOffer=p2.specialOffer,
  costPrice=p2.costPrice,marketPrice=p2.marketPrice,salePrice=p2.salePrice,upselling=p2.upselling,
  stock=p2.standard,importDate=p2.Inputdate,vistiCounts=p2.vistiCounts,sort_id=p2.ifdiscount,updateDate=updateDate,
  deleted=p2.deleted
       from  (select
         p.categoryid,'' as 'brandId' ,'' as 'sku',p.name,'' as 'erpName',p.shortDesc,p.unit1_id,p.unit2_id,'' as 'weight',
         '' as 'description',
         '' as 'seo_title','' as 'seo_keywords',p.comment,'' as 'recommended',p.isNew,p.isHot,0  as 'specialOffer',
         0 as 'costPrice',0 as 'marketPrice','' as 'salePrice' ,'' as 'upselling',p.standard, p.Inputdate,'' as 'vistiCounts',p.ifdiscount,p.deleted
         ,p.Integral
          from products p inner join  (select * from inserted ii where  ii.serial_number not in 
          (select serial_number from wx_shop_product)  ) p1  on p1.serial_number=p.serial_number) as p2
     where serial_number in (select serial_number from inserted )
          
         end
go
原文地址:https://www.cnblogs.com/superMay/p/5848476.html