识别和禁用链接到特定表的所有触发器

时间:2018-08-28 18:08:21

标签: sql-server tsql

我遇到以下问题:

我正在将数据插入表中,但是,我想禁用将来在此表上创建的触发器,这将禁止我的插入操作不起作用。

如果您在下面检查我的代码:

create trigger kv_trg_AssetAdjustment_AW on _btblInvoiceLines       --select * from _btblInvoiceLines
with encryption
after insert, update
as

if trigger_nestlevel() > 5
     return

BEGIN

    declare @Asset      varchar(40)
    ,       @Desc       varchar(100)
    ,       @AssetValue float
    ,       @Price      float
    ,       @Qty        float
    ,       @Code       varchar(100)
    ,       @Valid      int
    ,       @Exists     int                                             ------          NEXT STEP IS TO CHECK WHAT TRIGGERS ON ARE ON THIS TABLE TO DISABLE THEM!
    ,       @SL         int
    ,       @NextNum    int
    ,       @Variable   varchar(100)
    ,       @NewCode    varchar(100)
    ,       @Err        nvarchar(500)

    select  @Err    =   '--------------------------';
    select  @Err    =   @Err    +   @Err    +   CHAR(10);
    select  @Err    =   @Err    +   CHAR(10);
    select  @Err    =   @Err    +   'Please specify a hyphen ("-") between Item Code & Description in the Description!';

    select
        @Asset      =   ulIDPOrdTxGLVAT
    ,   @Code       =   LEFT(cDescription, CHARINDEX('-', cDescription) - 1)
    ,   @Desc       =   REPLACE(SUBSTRING(cDescription, CHARINDEX('-', cDescription), LEN(cDescription)), '-', '')
    ,   @AssetValue =   fQuantityLineTotExcl
    ,   @Qty        =   fQuantity
    from    inserted


    if exists(select Code from StkItem where Code = @Code)
        select  @Exists =   1
    else
        select  @Exists =   0

    if exists(select cDescription from inserted where cDescription  like '%-%')
        select  @Valid  =   1
    else
        begin
            raiserror(@Err, 16, 1)
            return;
        end

    select  @Variable   =   (select substring(@Code,1,len(@Code)-1))
    select  @NextNum    =   (select max(RIGHT(@Code,1))+1 from StkItem where Code like (@Variable + '%'))
    select  @NewCode    =   substring(@Code,1,len(@Code)-1) + cast(@NextNum as varchar)

    begin
        if  (@Asset = 'Asset')
            begin
                if  (@Exists = 0)
                    begin
                        if exists(select @Valid)
                            begin
                                alter table StkItem disable trigger trgStkItemNegStock;
                                    insert into StkItem     (
                                            Code
                                    ,       cSimpleCode
                                    ,       Description_1
                                    ,       Description_2
                                    ,       TTI
                                    ,       TTC
                                    ,       TTG
                                    ,       TTR
                                    ,       WhseItem
                                                            )
                                    select  @Code
                                    ,       @Code
                                    ,       @Desc
                                    ,       @AssetValue
                                    ,       1
                                    ,       1
                                    ,       1
                                    ,       1
                                    ,       1
                                alter table StkItem enable trigger trgStkItemNegStock;
                            end
                        print 'The Item Code you specified did not change.'
                    end
                else
                    begin
                        if exists(select @Valid)
                            begin
                                alter table StkItem disable trigger trgStkItemNegStock;
                                    insert into StkItem     (
                                            Code
                                    ,       cSimpleCode
                                    ,       Description_1
                                    ,       Description_2
                                    ,       TTI
                                    ,       TTC
                                    ,       TTG
                                    ,       TTR
                                    ,       WhseItem
                                                            )
                                    select  @NewCode
                                    ,       @NewCode
                                    ,       @Desc
                                    ,       @AssetValue
                                    ,       1
                                    ,       1
                                    ,       1
                                    ,       1
                                    ,       1
                                alter table StkItem enable trigger trgStkItemNegStock;
                            end
                        print 'The Item Code you specified has changed to : "' + @NewCode + '". Please write this down for record purposes.'
                    end
            end
            begin
                update  _btblInvoiceLines
                    set ufIDPOrdTxGLAssetValue          =   @AssetValue
                from    _btblInvoiceLines   L
                join    inserted on L.idInvoiceLines    =   inserted.idInvoiceLines
            end
            begin
                if (@Exists =   0)
                    begin
                        select @SL  =   (select top 1 StockLink from StkItem where Code =   @Code order by StockLink desc)

                        exec _bspWhUtilLinkStkToWH;1 @SL,1,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,2,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,3,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,4,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,6,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,7,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,8,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,9,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,10,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,11,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,12,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,13,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,14,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,15,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,16,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,17,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,18,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,19,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,20,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,21,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,22,0,0,1
                    end
                else
                    begin
                        select @SL  =   (select top 1 StockLink from StkItem where Code =   @NewCode order by StockLink desc)

                        exec _bspWhUtilLinkStkToWH;1 @SL,1,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,2,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,3,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,4,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,6,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,7,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,8,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,9,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,10,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,11,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,12,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,13,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,14,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,15,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,16,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,17,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,18,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,19,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,20,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,21,0,0,1
                        exec _bspWhUtilLinkStkToWH;1 @SL,22,0,0,1
                    end
            end
    end
END
go

如果您检查我在哪里禁用并启用触发器。我的意图是禁用此表上的所有触发器。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

要禁用表格上的所有触发器,请执行以下操作:

DISABLE TRIGGER ALL ON TableName;

要启用表格上的所有触发器,

ENABLE TRIGGER ALL ON TableName;