sqlserver trigger

來源:互聯網
上載者:User

標籤:

 1 --==================================== 2 --  Create database trigger template  3 --==================================== 4 USE [EasyJobExTest] 5 GO 6  7 --判斷觸發器是否存在,存在則刪除 8 IF EXISTS( 9   select top 1 t.name as trigger_name,a.name as table_name from sys.triggers t,sys.objects a 10 where Lower(a.name)=‘section‘ and t.parent_id=a.object_id and Lower(t.name)=‘tr_section_insert‘11 )12 begin13     DROP TRIGGER tr_section_insert;14 end15 GO16 17 /*before觸發器instead of*/18 CREATE TRIGGER tr_section_insert ON section 19     instead of insert,delete,update20     --for delete --after觸發器delete21     --for insert --after觸發器insert22     --for update --after觸發器update23 AS 24 begin25 declare @SecId    int;26 declare @SecName    nvarchar(50);27 declare @Remark    nvarchar(200);28 declare @Department    int;29 declare @CreateTime    datetime;30 declare @Deepness    int;31 declare @ManageUserID    int;32 33 select @SecId=secid,@SecName=t.SecName,@Remark=t.Remark,@Department=t.Department,@CreateTime=t.CreateTime,34 @Deepness=t.Deepness,@ManageUserID=t.ManageUserID from inserted t;35 36 if exists(select top 1 * from inserted)37 begin38 print ‘trigger insert‘39     IF @CreateTime is NULL40     begin41         select @CreateTime = Sysdatetime()42         print ‘目前時間:‘43         print @CreateTime44     end45         insert into Section values(@SecId,@SecName,@Remark,@Department,@CreateTime,@Deepness,@ManageUserID);46 end47 else if exists(select top 1 * from deleted)48 begin49 print ‘trigger delete‘50 end51 else52 print ‘trigger update‘53 54 /*55 IF IS_MEMBER (‘db_owner‘) = 056 BEGIN57    PRINT ‘You must ask your DBA to drop or alter tables!‘ 58    ROLLBACK TRANSACTION59 END60 */61 end62 go

 

sqlserver trigger

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.