SQL Server Trigger

Source: Internet
Author: User

1 --====================================2 --Create Database Trigger template3 --====================================4  Use [easyjobextest]5 GO6 7 --determine if a trigger exists, delete it8 IF EXISTS(9   Select Top 1T.name asTrigger_name,a.name astable_name fromsys.triggers t,sys.objects aTen where Lower(A.name)=' Section'  andt.parent_id=A.object_id  and Lower(T.name)='Tr_section_insert' One ) A begin -     DROP TRIGGERTr_section_insert; - End the GO -  - /*before trigger instead of*/ - CREATE TRIGGERTr_section_insert on Section +instead of Insert,Delete,Update -     --For Delete--after trigger Delete +     --For insert--after trigger Insert A     --For update--after trigger update at  as  - begin - Declare @SecId    int; - Declare @SecName    nvarchar( -); - Declare @Remark    nvarchar( $); - Declare @Department    int; in Declare @CreateTime    datetime; - Declare @Deepness    int; to Declare @ManageUserID    int; +  - Select @SecId=Secid,@SecName=T.secname,@Remark=T.remark,@Department=T.department,@CreateTime=T.createtime, the @Deepness=T.deepness,@ManageUserID=T.manageuserid frominserted t; *  $ if exists(Select Top 1 *  frominserted)Panax Notoginseng begin - Print 'Trigger Insert' the     IF @CreateTime  is NULL +     begin A         Select @CreateTime =sysdatetime () the         Print 'Current Time:' +         Print @CreateTime -     End $         Insert  intoSectionValues(@SecId,@SecName,@Remark,@Department,@CreateTime,@Deepness,@ManageUserID); $ End - Else if exists(Select Top 1 *  fromdeleted) - begin the Print 'Trigger Delete' - EndWuyi Else the Print 'Trigger Update' -  Wu /* - IF is_member (' db_owner ') = 0 About BEGIN $ PRINT ' must ask your DBA to drop or alter tables! ' - ROLLBACK TRANSACTION - END - */ A End + Go

SQL Server Trigger

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.