如何在所有记录上激活触发器?

时间:2018-12-19 16:33:52

标签: sql-server triggers

我在使用查询设置值的表上有一个UPDATE / INSERT触发器。现在查询已更改,我想在每条记录上运行触发器。

是否有一种简单的方法来激活表中每个记录的触发器?我尝试了一个简单的更新,例如

update INDIVIDUAL_LOC
set EDIT_LINK=1

但是这不会激活触发器。

触发代码为:

DECLARE
    @sCountry   Varchar(254),
    @sLeftPostcode  Varchar(10),
    @sPartPostcode  Varchar(10),
    @nIndRef    Integer,
    @sAttrCode  Varchar(254),
    @nAttrCodeRef   Integer

BEGIN
IF EXISTS (SELECT 1 FROM app_profile WHERE section = 'TRIGGERS' AND entry = 'RGS Region Attribute Trigger 2' AND value = 'Y' )
    BEGIN
    --add check for validity of address
    IF EXISTS (SELECT 1 FROM inserted WHERE inserted.main_location = 'Y' AND inserted.valid_from <= getdate() AND (inserted.valid_to > getdate() OR inserted.valid_to IS NULL))
        BEGIN
        SELECT @sCountry = location.country, @sLeftPostcode = LEFT(location.postcode,2), @sPartPostcode = SUBSTRING(location.postcode, 0, CHARINDEX(' ', location.postcode)), @nIndRef = inserted.individual_ref
        FROM inserted, location
        WHERE inserted.location_ref = location.location_ref

        IF @sCountry = 'Singapore'
            SET @sAttrCode = 'SING'
        ELSE IF @sCountry = 'Hong Kong'
            SET @sAttrCode = 'HK'
        ELSE IF @sCountry = 'Republic of Ireland'
            SET @sAttrCode = 'IRL'
        ELSE IF @sCountry = 'UK'
            BEGIN
            SELECT @sAttrCode = lookup_short_desc
            FROM lookup, lookup_type
            WHERE lookup_type.lookup_type_ref = lookup.lookup_type_ref
            AND lookup_type.lookup_type_code = 'PCODE2'
            AND lookup.lookup_code = @sLeftPostcode
            AND lookup.valid_from <= getdate() AND (lookup.valid_to >= getdate() OR lookup.valid_to IS NULL)

            IF @sAttrCode IS NULL
                SELECT @sAttrCode = lookup_short_desc
                FROM lookup, lookup_type
                WHERE lookup_type.lookup_type_ref = lookup.lookup_type_ref
                AND lookup_type.lookup_type_code = 'PCODE2'
                AND lookup.lookup_code = @sPartPostcode
                AND lookup.valid_from <= getdate() AND (lookup.valid_to >= getdate() OR lookup.valid_to IS NULL)
            END

        IF @sAttrCode IS NOT NULL
            SELECT @nAttrCodeRef = lookup_ref
            FROM lookup
            WHERE lookup_type_ref = 339
            AND lookup_code = @sAttrCode
            AND lookup.valid_from <= getdate() AND (lookup.valid_to >= getdate() OR lookup.valid_to IS NULL)
        ELSE
            SELECT @nAttrCodeRef = lookup_ref
            FROM lookup
            WHERE lookup_type_ref = 339
            AND lookup_code = 'NOREG'
            AND lookup.valid_from <= getdate() AND (lookup.valid_to >= getdate() OR lookup.valid_to IS NULL)

        IF @nAttrCodeRef IS NOT NULL
            BEGIN
            --Check whether this exact attribute already exists or not
            IF NOT EXISTS (SELECT 1 FROM attribute WHERE individual_ref = @nIndRef AND attr_code_ref = @nAttrCodeRef)
                BEGIN
                --Check whether the attribute already exists or not
                IF EXISTS (SELECT 1 FROM attribute WHERE individual_ref = @nIndRef AND code_type = 339)
                    UPDATE attribute SET attribute_code = @sAttrCode, attr_code_ref = @nAttrCodeRef, valid_to = null WHERE individual_ref = @nIndRef AND code_type = 339
                ELSE
                    INSERT INTO attribute ( individual_ref, code_type, attribute_code, attr_code_ref, valid_from )
                    VALUES ( @nIndRef, 339, @sAttrCode, @nAttrCodeRef, getdate() )
                END
            END

        END
    END
END

0 个答案:

没有答案