DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2005
    Posts
    44

    UPDATE or INSERT in a trigger?

    In a SQL Server trigger for UPDATE INSERT, how can I determine which of the two is happening? The IF UPDATE() function is run in both cases.

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    That would be based on something external to the trigger.

    What do you have that would fire the trigger off?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Oct 2005
    Location
    Maady
    Posts
    1,819
    you can SELECT and use "CASE" to insert or update ... better if you have support for PL/SQL .. or any high level language for example ..
    Programmer&Cracker CS
    MyBlog:Blog.Amahdy.com
    MyWebsite:www.Amahdy.com

  4. #4
    Join Date
    Feb 2008
    Posts
    162
    I'm not an expert on triggers by any means, but are you checking the "inserted" and "updated" tables in your code?
    It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain

  5. #5
    Join Date
    Sep 2005
    Posts
    44

    UPDATE or INSERT in a trigger?

    When inserting records in the table OR updating a column to a certain value (eg. "TRUE") inside a record, I want to automatically insert some records into other tables as well. In any other case, I don't want to do anything.

    The inserted table is used both in update and insert.

  6. #6
    Join Date
    Sep 2005
    Posts
    44
    Quote Originally Posted by Hack
    That would be based on something external to the trigger.

    What do you have that would fire the trigger off?
    What do you mean? A trigger can't read or get passed any variable outside it. (if understand you correctly)

  7. #7
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    You can set up a trigger to fire off on an update, or an insert, or a delete, or any other kind of database change.

    http://www.sqlteam.com/article/an-in...riggers-part-i
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  8. #8
    Join Date
    Feb 2008
    Posts
    162
    Quote Originally Posted by iordanis
    When inserting records in the table OR updating a column to a certain value (eg. "TRUE") inside a record, I want to automatically insert some records into other tables as well. In any other case, I don't want to do anything.

    The inserted table is used both in update and insert.
    It sounds like you need to use the INSTEAD OF clause and then do whatever you need to do to all tables you need to touch.

    http://msdn2.microsoft.com/en-us/lib...4(SQL.80).aspx

    What version of SQL Server? It might be helpful if you were to post some code.
    It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain

  9. #9
    Join Date
    Sep 2005
    Posts
    44

    UPDATE or INSERT in a trigger?

    I just came up with this idea. If the count of records in the deleted table is >0 then an update must have been taken place, otherwise an insert. Could this be feasible (I haven't tried this yet, but I will shortly)

  10. #10
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,649
    Actually, it sounds like a classic trigger situation and solution.
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  11. #11
    Join Date
    Feb 2008
    Posts
    162
    EDIT: My bad. There I go getting myself in trouble again.

    Okay. I finally got curious enough to start playing with this. I see what you mean now about how the IF UPDATE() returns True for both inserts and updates.

    There may be a better way to do this, but what seems to work is -
    a) Check for records in the inserted table that already exist, then do the update procedures for those.
    b) Check for records in the inserted table that do not exist and do the insert procedures for those.


    An interesting thing about doing this is that it causes INSERTs to behave like the MERGE statement in Oracle (and SQL 2008). The trigger just handles updates for records that already exist and then inserts the rest.


    Here is my test code.

    Code:
    CREATE TRIGGER trg_myTriggerTest
    ON myTriggerTest
    INSTEAD OF INSERT, UPDATE
    AS
    BEGIN
        DECLARE @recs INT
    
        -- Look for records in the inserted table that already exist...
        SELECT @recs = COUNT(*)
        FROM myTriggerTest a
        JOIN inserted i ON i.myID = a.myID
    
        IF @Recs > 0 
            UPDATE myTriggerTest SET 
                myVal = i.myVal,
                myComment = 'UPDATED'
            FROM myTriggerTest a
            JOIN inserted i ON i.myID = a.myID
    
    
    
        -- Now look for records in the inserted table that don't exist...
        SELECT @recs = COUNT(*)
        FROM inserted i 
        LEFT JOIN myTriggerTest a ON a.myID = i.myID
        WHERE a.myID IS NULL
    
        IF @Recs > 0
            INSERT INTO myTriggerTest(myID, myVal, myComment)
            SELECT i.myID, i.myVal, 'INSERTED'
            FROM inserted i 
            LEFT JOIN myTriggerTest a ON a.myID = i.myID
            WHERE a.myID IS NULL
    
    END
    Code:
    INSERT INTO myTriggerTest(myID, myVal)
    SELECT 1, 'Test' UNION
    SELECT 2, 'Test' UNION
    SELECT 3, 'Test' UNION
    SELECT 4, 'Test' UNION
    SELECT 5, 'Test'
    
    
    
    INSERT INTO myTriggerTest(myID, myVal)
    SELECT 1, 'Test' UNION
    SELECT 2, 'Test' UNION
    SELECT 3, 'Test' UNION
    SELECT 4, 'Test' UNION
    SELECT 5, 'Test' UNION
    SELECT 6, 'Test' UNION
    SELECT 7, 'Test' UNION
    SELECT 8, 'Test' UNION
    SELECT 9, 'Test' UNION
    SELECT 10, 'Test' UNION
    SELECT 11, 'Test' UNION
    SELECT 12, 'Test' UNION
    SELECT 13, 'Test' UNION
    SELECT 14, 'Test' UNION
    SELECT 15, 'Test' UNION
    SELECT 16, 'Test' UNION
    SELECT 17, 'Test' UNION
    SELECT 18, 'Test' UNION
    SELECT 19, 'Test' UNION
    SELECT 20, 'Test'
    Last edited by Slope; 03-18-2008 at 05:32 PM.
    It's not what you don't know that gets you in trouble. It's what you know that just ain't so. -Mark Twain

Similar Threads

  1. SQL Help
    By carbon_13 in forum Database
    Replies: 14
    Last Post: 03-14-2008, 04:03 PM
  2. how to clone database using mysql
    By zarien in forum Database
    Replies: 1
    Last Post: 05-17-2006, 03:27 AM
  3. Re: Delay in Insert Trigger - My mistake !
    By Steve in forum Database
    Replies: 0
    Last Post: 02-19-2003, 04:51 AM
  4. Delay in Insert Trigger
    By Steve in forum Database
    Replies: 0
    Last Post: 02-19-2003, 02:14 AM
  5. SQL Tutorial (Answer Q's & post your reply)
    By bigbastard4 in forum Database
    Replies: 2
    Last Post: 05-16-2001, 06:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links