zoukankan      html  css  js  c++  java
  • Create trigger


    -- =============================================
    -- Create trigger basic template(After trigger)
    -- =============================================
    IF EXISTS (SELECT name
           FROM   sysobjects
           WHERE  name = N'<trigger_name, sysname, trig_test>'
           AND       type = 'TR')
        DROP TRIGGER <trigger_name, sysname, trig_test>
    GO

    CREATE TRIGGER <trigger_name, sysname, trig_test>
    ON <table_name, sysname, pubs.dbo.sales>
    FOR DELETE, INSERT, UPDATE
    AS
    BEGIN
        RAISERROR (50009, 16, 10)
    END
    GO

    -- =============================================
    -- Create trigger contained If UPDATE(column)
    -- =============================================
    IF EXISTS (SELECT name
           FROM   sysobjects
           WHERE  name = N'<trigger_name, sysname, trig_test>'
           AND       type = 'TR')
        DROP TRIGGER <trigger_name, sysname, trig_test>
    GO

    CREATE TRIGGER <trigger_name, sysname, trig_test>
    ON <table_or_view_name, sysname, pubs.dbo.sales>
    FOR INSERT, UPDATE
    AS
    If UPDATE(<column_1, sysname, stor_id>) OR UPDATE(<column_2, sysname, ord_num>)
    BEGIN
        RAISERROR ('These columns should never be updated', 16, 1)
        ROLLBACK TRANSACTION
    END
    GO

    -- =============================================
    -- Create trigger contained If (COLUMNS_UPDATED())
    -- =============================================
    IF EXISTS (SELECT name
           FROM   sysobjects
           WHERE  name = N'<trigger_name, sysname, trig_test>'
           AND       type = 'TR')
        DROP TRIGGER <trigger_name, sysname, trig_test>
    GO

    CREATE TRIGGER <trigger_name, sysname, trig_test>
    ON <table_or_view_name, sysname, pubs.dbo.sales>
    FOR INSERT, UPDATE
    AS
    --eg. check if all of column 2, 3, 4 are updated
    IF (COLUMNS_UPDATED() & 14) = 14
    BEGIN
        RAISERROR ('These columns can not be updated at the same time', 16, 1)
        ROLLBACK TRANSACTION
    END
    GO

    -- =============================================
    -- Create basic Instead Of Trigger
    -- =============================================
    IF EXISTS (SELECT name
           FROM   sysobjects
           WHERE  name = N'<trigger_name, sysname, trig_test>'
           AND       type = 'TR')
        DROP TRIGGER <trigger_name, sysname, trig_test>
    GO

    CREATE TRIGGER <trigger_name, sysname, trig_test>
    ON <table_or_view_name, sysname, pubs.dbo.sales>
    INSTEAD OF INSERT
    AS
    BEGIN
        RAISERROR (50009, 16, 10)
        EXEC sp_who
    END
    GO

  • 相关阅读:
    Java服务器 dm Server
    POI
    系统程序员成长计划写得又快又好的秘诀(四)
    _stati64()获取文件信息
    系统程序员成长计划并发(一)(下)
    KJAVA虚拟机Hack笔记用GTK+实现绘图操作
    系统程序员成长计划写得又快又好的秘诀(六)
    系统程序员成长计划并发(一)(上)
    系统程序员成长计划并发(二)(上)
    系统程序员成长计划并发(二)(下)
  • 原文地址:https://www.cnblogs.com/mingyongcheng/p/1995132.html
Copyright © 2011-2022 走看看