Results 1 to 9 of 9

Thread: Trigger in SQL

  1. #1
    Join Date
    Oct 2008
    Posts
    3

    Unanswered: Trigger in SQL

    Hi, I'm new to SQL Server 2005 and I have a question regrading triggering.
    I have a table A with fields: A_Message, B_ID
    And table B with fields: B_ID, B_Message
    and table C with fields: A_Message, B_Message

    And I want that every time that a record is inserted to table A, then a new row will be inserted to table C.

    Here is what I wrote:

    Alter trigger dbo.trigger_temp
    ON TableA
    FOR INSERT
    AS
    BEGIN
    INSERT TableC VALUES(?????)
    END

    I don't know how to tell the trigger from where to take the data, how to retrieve it from the new row. Can you help me?
    Thanks a lot:
    Mor

  2. #2
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    Look in Books OnLine at triggers.

    Pay particular attention to the inserted and deleted logical tables.
    George
    Home | Blog

  3. #3
    Join Date
    Oct 2008
    Posts
    3
    Thanks, but can you give me more detailed, I cannot find the correct information there....

  4. #4
    Join Date
    Jan 2007
    Location
    UK
    Posts
    11,434
    Provided Answers: 10
    George
    Home | Blog

  5. #5
    Join Date
    Oct 2008
    Posts
    3
    Thanks :-)

  6. #6
    Join Date
    Dec 2007
    Location
    London, UK
    Posts
    741
    Unfortunately some of the code snippets in those links are very bad examples. The one called "Purchasing.LowCredit" for example will fail to work correctly if more than one row is updated. Never be tempted to write a trigger like that.

    See also: http://msdn.microsoft.com/en-us/libr...2(SQL.90).aspx

  7. #7
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    Wow. That's pretty sad that Microsoft would have that in one of their code examples. A noob mistake.
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  8. #8
    Join Date
    May 2004
    Location
    Seattle
    Posts
    1,313
    who writes code examples? so called "documentation devs". you think the best devs are lining up for that job? not really...

  9. #9
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    To be fair, it isn't strictly correct to say it won't work correctly if 2+ rows are inserted. Sometimes it will work correctly, sometimes it will not. Although you might quibble with what "working correctly" would actually be.

    Of course this is simply Worse Than Failure.
    Testimonial:
    pootle flump
    ur codings are working excelent.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •