SQL触发器

插入触发器:

if (object_id('tgr_Category_insert', 'tr') is not null)

    drop trigger tgr_Category_insert

go

create trigger tgr_Category_insert

on Category

    for insert --插入触发

as

    --定义变量

    declare @CGID int,@CGType tinyint;

    --在inserted表中查询已经插入记录信息

    select @CGID = CGID,@CGType=CGType from inserted;

    if(@CGType=1)

    begin

        insert into Advertisements (CGFstID,CurrentAdvert,ModifyDate,Operator,ImgUrl,ImgGotoUrl,AdvertCode)

        values (@CGID,0,getdate(),'','','','')

    end   

go

删除触发器:

if (object_id('tgr_Category_delete', 'tr') is not null)

    drop trigger tgr_Category_delete  

go

create trigger tgr_Category_delete 

on Category  

    for delete --删除触发  

as  

    --定义变量  

    declare @CGID int,@CGType tinyint;  

    select @CGID = CGID,@CGType=CGType from deleted;  

     if(@CGType=1)  

     begin  

      delete from Advertisements where CGFstID=@CGID  

     end       

go

 

查询触发器语句:

use jxcSoftware --数据库名称

go

select * from sysobjects where xtype='TR'

 

你可能感兴趣的:(sql)