我如何更新触发器以进行多行插入,更新和删除?

问题描述 投票:0回答:1

我正在为在主表上执行的任何操作创建一个新触发器,我需要使用用于插入,更新和删除的触发器来更新本地表。但是现在我在多行插入或更新期间面临问题。

我创建了触发器,但是对于多行插入和更新它无法正常工作。

CREATE TRIGGER [dbo].[TRG_pspartmas] ON [dbo].[ps_partmas]
AFTER INSERT
    ,UPDATE
    ,DELETE
AS
BEGIN
    DECLARE @cmp_id VARCHAR(10)
    DECLARE @sl_type VARCHAR(10)
    DECLARE @sl_code VARCHAR(20)
    DECLARE @sl_desc VARCHAR(200)
    DECLARE @region_code VARCHAR(10)
    DECLARE @tranYN VARCHAR(10)
    DECLARE @IT_NO VARCHAR(50)

    ------------------------------------------------------------------------------Fetching recored from inserted
    -- SELECT @cmp_id=cmp_id,@sl_type=cl_supp_code,@sl_code=part_code,@sl_desc=part_nm,@region_code=region_code,@tranYN=Stop_Order,@it_NO=IT_no
    -- FROM inserted
    --------------------------------------------------------------------------------if recored were deletd
    SELECT @cmp_id = rtrim(ltrim(cmp_id))
        ,@sl_code = rtrim(ltrim(part_code))
        ,@sl_desc = rtrim(ltrim(part_nm)) --,@tranYN=rtrim(ltrim(Stop_Order))
    FROM deleted

    IF EXISTS (
            SELECT *
            FROM view_sl_code1
            WHERE cmp_id = rtrim(ltrim(@cmp_id))
                AND sl_code = rtrim(ltrim(@sl_code))
                AND sl_type = @sl_type
            )
    BEGIN
        BEGIN
            DELETE
            FROM view_sl_code1
            WHERE cmp_id = rtrim(ltrim(@cmp_id))
                AND sl_code = rtrim(ltrim(@sl_code))
                AND sl_type = rtrim(ltrim(@sl_type))
        END
    END

    SELECT @cmp_id = cmp_id
        ,@sl_type = cl_supp_code
        ,@sl_code = part_code
        ,@sl_desc = part_nm
        ,@region_code = region_code
        ,@tranYN = Stop_Order
        ,@it_NO = IT_no
    FROM inserted

    --@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    IF EXISTS (
            SELECT *
            FROM view_sl_code1
            WHERE cmp_id = rtrim(ltrim(@cmp_id))
                AND sl_code = rtrim(ltrim(@sl_code))
                AND sl_type = @sl_type
            )
    BEGIN
        UPDATE view_sl_code1
        SET sl_desc = rtrim(ltrim(@sl_desc))
            ,tranYN = rtrim(ltrim(@tranYN))
            ,actions = 'partmas_up'
            ,date_update = GETDATE()
        WHERE cmp_id = rtrim(ltrim(@cmp_id))
            AND sl_code = rtrim(ltrim(@sl_code))
    END

    ------------------------------------------------------------------------------inserting new recored
    IF NOT EXISTS (
            SELECT *
            FROM view_sl_code1
            WHERE cmp_id = rtrim(ltrim(@cmp_id))
                AND sl_code = rtrim(ltrim(@sl_code))
                AND sl_type = @sl_type
            )
    BEGIN
        INSERT INTO view_sl_code1 (
            cmp_id
            ,sl_type
            ,sl_code
            ,sl_desc
            ,region_code
            ,tranYN
            ,IT_NO
            ,actions
            ,date_update
            )
        SELECT rtrim(ltrim(cmp_id))
            ,rtrim(ltrim(cl_supp_code)) AS sl_type
            ,rtrim(ltrim(part_code)) AS sl_code
            ,rtrim(ltrim(part_nm)) AS sl_desc
            ,rtrim(ltrim(region_code))
            ,CASE 
                WHEN Stop_Order = 'Y'
                    THEN 'N'
                ELSE 'Y'
                END AS tranYN
            ,IT_NO
            ,'partmas_in'
            ,getdate()
        FROM dbo.ps_partmas
        WHERE cmp_id = @cmp_id
            AND part_code = @sl_code
            AND cl_supp_code = @sl_type
    END

    --end
    PRINT 'Trigger is activated'
END

我需要捕获所有捕获本地表中所有插入和更新的值。

您能建议吗?

sql sql-server triggers database-trigger sql-query-store
1个回答
0
投票

您只需简单地连接到插入和删除的表。

这里是一个例子。我已删除所有ltrim / rtrim以提高可读性,如果需要,请重新添加。

delete view_sl_code1
from view_sl_code1
join deleted 
    on deleted.cmp_id=view_sl_code1.cmp_id
    and deleted.sl_code=view_sl_code1.sl_code
    and deleted.sl_desc=view_sl_code1.sl_desc

update view_sl_code1
    SET sl_desc = inserted.sl_desc
    ,tranYN = inserted.tranYN
    ,actions = 'partmas_up'
    ,date_update = GETDATE()
from view_sl_code1
join inserted 
    on inserted.cmp_id = view_sl_code1.cmp_id
    AND inserted.sl_code = view_sl_code1.sl_code

insert view_sl_code1 (
        cmp_id
        ,sl_type
        ,sl_code
        ,sl_desc
        ,region_code
        ,tranYN
        ,IT_NO
        ,actions
        ,date_update
        )
select cmp_id,
    cl_supp_code AS sl_type
    ,part_code AS sl_code
    ,part_nm AS sl_desc
    ,region_code
    ,CASE 
        WHEN Stop_Order = 'Y'
            THEN 'N'
        ELSE 'Y'
        END AS tranYN
    ,IT_NO
    ,'partmas_in'
    ,getdate()
FROM dbo.ps_partmas
join inserted 
    on inserted.cmp_id = ps_partmas.cmp_id
    AND inserted.part_code = ps_partmas.sl_code
    AND inserted.cl_supp_code = ps_partmas.sl_type
where not exists(
    SELECT *
    FROM view_sl_code1
    join inserted
        on inserted.cmp_id = view_sl_code1.cmp_id
        AND inserted.sl_code = view_sl_code1.sl_code
        AND inserted.sl_type = view_sl_code1.sl_type        
)
© www.soinside.com 2019 - 2024. All rights reserved.