[转]MSSQL数据库触发器--更新(update),删除(delete),插入(insert)

创建studentcourse(学号,姓名,课程1,课程2...)        information(学号,姓名,性别,籍贯,联系电话....) 两张表

更新型触发器(update):

Create Trigger T1
            On studentcourse                              --在Studentcourese表中创建触发器
            for Update                               --为什么事件触发
          As                                             --事件触发后所要做的事情
            if Update(姓名)           
            begin

              Update information 
                Set 姓名=i.姓名
                From information inf, Deleted        d ,Inserted i           --Deleted和Inserted临时表
                Where inf.学号=d.学号

            end

删除型触发器(delete):

Create trigger T2
            On Studentcourse
            for Delete
          As
            Delete information 
              From information inf, Deleted d
              Where inf.学号=d.学号

插入型触发器(insert):

Create trigger T3
            On Studentcourse
            for INSERT
          As
            INSERT into information(学号,姓名)

           select i.学号,i.姓名 
           from studentcourse sc,inserted i
           where sc.学号=i.学号

------------------------------------------------------------------------

create trigger tr_ZTA_HRDNET_ins on ZTA_HRDNET for insert as set nocount on insert employee(pernr,password) select pernr, '123456' from inserted set nocount off go create trigger tr_ZTA_HRDNET_del on ZTA_HRDNET for delete as set nocount on delete employee where pernr in (select pernr from deleted) set nocount off go create trigger tr_ZTA_HRDNET_upd on ZTA_HRDNET for update as set nocount on update employee set password = ins.password from employee emp join inserted ins on emp.pernr=ins.pernr set nocount off go

posted @ 2011-07-28 12:31  jayccc  阅读(2686)  评论(0编辑  收藏  举报