在Insert语句的values部分中使用Case语句

时间:2010-04-15 21:47:54

标签: sql sql-update case-statement

请原谅我的无知和糟糕的SQL编程技巧,但我通常是一名基本的SQL开发人员。

我需要创建一个触发器,在一个表中插入数据,以将不同的数据插入另一个表中。

在此触发器中,我需要根据原始表中新插入的数据中的值将某些数据插入到新表中。我对这完全感到困惑。我认为我会很有创意并且在值部分中使用案例陈述但它不起作用。

有人可以帮我这个吗? (下面是我现在拥有的触发器的代码)

    INSERT INTO dbo.WebOnlineUserPeopleDashboard
    (
        ONLINE_USERACCOUNT_ID,
        ONLINE_ROOMS_DIRECTORY,
        ONLINE_ROOMS_LIST,
        ONLINE_ROOMS_PLACEMENT,
        ONLINE_ROOMS_MANAGEMENT,
        ONLINE_MAILINGLIST_DIRECTORY,
        ONLINE_MAILINGLIST_LIST,
        ONLINE_MAILINGLIST_MEMBERS,
        ONLINE_MAILINGLIST_MANAGER,
        ONLINE_PEOPLESEARCH_DIRECTORY
    )
VALUES
    IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 1
        BEGIN
            SELECT
                ONLINE_USERACCOUNT_ID,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1,
                1
            FROM INSERTED
        END
    ELSE IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 0
        BEGIN
            SELECT
                ONLINE_USERACCOUNT_ID,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0
            FROM INSERTED
        END
    ELSE
        BEGIN
            SELECT
                ONLINE_USERACCOUNT_ID,
                CASE --DIRECTORY
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0 
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
                        THEN 0
                END,
                CASE
                    WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1
                        THEN 1
                    WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0
                        THEN 0
                END
            FROM INSERTED
        END 
    END

2 个答案:

答案 0 :(得分:2)

如果触发器影响了多行,这将处理所有行:

INSERT INTO dbo.WebOnlineUserPeopleDashboard
    (
        ONLINE_USERACCOUNT_ID,
        ONLINE_ROOMS_DIRECTORY,
        ONLINE_ROOMS_LIST,
        ONLINE_ROOMS_PLACEMENT,
        ONLINE_ROOMS_MANAGEMENT,
        ONLINE_MAILINGLIST_DIRECTORY,
        ONLINE_MAILINGLIST_LIST,
        ONLINE_MAILINGLIST_MEMBERS,
        ONLINE_MAILINGLIST_MANAGER,
        ONLINE_PEOPLESEARCH_DIRECTORY
    )
SELECT
    ONLINE_USERACCOUNT_ID,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS=1
UNION
SELECT
    ONLINE_USERACCOUNT_ID,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS=0
UNION
SELECT
    ONLINE_USERACCOUNT_ID,
    CASE --DIRECTORY
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
            THEN 1
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1
            THEN 1
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0 
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1
            THEN 1
        WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1
            THEN 1
        WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
            THEN 1
        WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1
            THEN 1
        WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1
            THEN 1
        WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
            THEN 1
        WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1
            THEN 0
        --ELSE ???  what is the default
    END,
    CASE
        WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1
            THEN 1
        WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0
            THEN 0
        --ELSE ???  what is the default
    END
FROM INSERTED
WHERE ONLINE_PEOPLE_FULL_ACCESS NOT IN (0,1)

答案 1 :(得分:0)

这样的东西?

IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 1

    INSERT INTO dbo.WebOnlineUserPeopleDashboard 
    ( 
        ONLINE_USERACCOUNT_ID, 
        ONLINE_ROOMS_DIRECTORY, 
        ONLINE_ROOMS_LIST, 
        ONLINE_ROOMS_PLACEMENT, 
        ONLINE_ROOMS_MANAGEMENT, 
        ONLINE_MAILINGLIST_DIRECTORY, 
        ONLINE_MAILINGLIST_LIST, 
        ONLINE_MAILINGLIST_MEMBERS, 
        ONLINE_MAILINGLIST_MANAGER, 
        ONLINE_PEOPLESEARCH_DIRECTORY 
    ) 
    VALUES 
    SELECT 
        ONLINE_USERACCOUNT_ID, 
        1, 
        1, 
        1, 
        1, 
        1, 
        1, 
        1, 
        1, 
        1 
    FROM INSERTED 

ELSE IF (SELECT ONLINE_PEOPLE_FULL_ACCESS FROM INSERTED) = 0 

    INSERT INTO dbo.WebOnlineUserPeopleDashboard 
    ( 
        ONLINE_USERACCOUNT_ID, 
        ONLINE_ROOMS_DIRECTORY, 
        ONLINE_ROOMS_LIST, 
        ONLINE_ROOMS_PLACEMENT, 
        ONLINE_ROOMS_MANAGEMENT, 
        ONLINE_MAILINGLIST_DIRECTORY, 
        ONLINE_MAILINGLIST_LIST, 
        ONLINE_MAILINGLIST_MEMBERS, 
        ONLINE_MAILINGLIST_MANAGER, 
        ONLINE_PEOPLESEARCH_DIRECTORY 
    ) 
    VALUES 
    SELECT 
        ONLINE_USERACCOUNT_ID, 
        0, 
        0, 
        0, 
        0, 
        0, 
        0, 
        0, 
        0, 
        0 
    FROM INSERTED 

ELSE 

    INSERT INTO dbo.WebOnlineUserPeopleDashboard 
    ( 
        ONLINE_USERACCOUNT_ID, 
        ONLINE_ROOMS_DIRECTORY, 
        ONLINE_ROOMS_LIST, 
        ONLINE_ROOMS_PLACEMENT, 
        ONLINE_ROOMS_MANAGEMENT, 
        ONLINE_MAILINGLIST_DIRECTORY, 
        ONLINE_MAILINGLIST_LIST, 
        ONLINE_MAILINGLIST_MEMBERS, 
        ONLINE_MAILINGLIST_MANAGER, 
        ONLINE_PEOPLESEARCH_DIRECTORY 
    ) 
        SELECT 
            ONLINE_USERACCOUNT_ID, 
            CASE --DIRECTORY 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_FULL_ACCESS = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_VIEW = 0  
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 1 OR ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_ROOMS_PLACEMENT_ADD = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_UPDATE = 0 AND ONLINE_PEOPLE_ROOMS_PLACEMENT_DELETE = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_ROOMS_MANAGEMENT_FULL_ACCESS = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 1 OR ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_FULL_ACCESS = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_VIEW = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_ADD = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_UPDATE = 0 AND ONLINE_PEOPLE_MAILING_LISTS_MEMBERS_DELETE = 0 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_MAILING_LISTS_ADD = 1 OR ONLINE_PEOPLE_MAILING_LISTS_UPDATE = 1 OR ONLINE_PEOPLE_MAILING_LISTS_DELETE = 1 
                    THEN 0 
            END, 
            CASE 
                WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 1 
                    THEN 1 
                WHEN ONLINE_PEOPLE_PEOPLE_SEARCH = 0 
                    THEN 0 
            END 
        FROM INSERTED