views:

185

answers:

2

Please forgive my ignorance and poor SQL programming skills but I am normally a basic SQL developer.

I need to create a trigger off the insertion of data in one table to insert different data into another table.

Within this trigger I need to insert certain data into the new table based upon values within the newly inserted data from the original table. I am totally confused on this. i thought I would be creative and use a case statement within teh Values section but it is not working.

Can anyone please help me on this? (below is the code for the trigger that I have as of now)

    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
+1  A: 

Something like this?

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 
Germ
Oh ok, so use an IF ELSE statement to encapsulate the entire insert statements. Completely understandable and logical. I will attempt that tomorrow morning when I get back into the office and let you know.Thank you very much.
mattgcon
if you ever insert multiple rows at one time, this trigger will fail.
KM
This worked perfectly thank you, This table is a single row insertion process so I will not have to worry about that for now. In my other tables i just might have to user the other example for multiple row insertions.
mattgcon
**NEVER code a trigger for single insertion** it will eventually bite you! perhaps you app inserts only one today, what about some time in the future, maybe a year for now you'll have some script insert a bunch or you'll import a file, etc. **this trigger will not fail with an error and a rollback, but you will just get wrong data, which is way worse!**
KM
+1  A: 

this will handle all rows if multiple rows are affected by the trigger:

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)
KM