Results 1 to 3 of 3

Thread: SQL Server insert triggers

  1. #1
    Join Date
    Jun 2003
    Location
    UK
    Posts
    4

    Question SQL Server insert triggers

    I want to use an insert trigger to take data from one field, modify it, then insert it into another field in the same row.

    I can do this with interbase, but I cannot figure out if it even possible in SQL Server. Can anyone help?

    FYI, this is what I have tried so far:

    CREATE TRIGGER SetMyNewCode
    ON MyTable
    AFTER INSERT AS
    BEGIN
    DECLARE @vNewCode VARCHAR(7)
    , @vOldCode VARCHAR(5)
    -- only act if code has been passed
    IF UPDATE (MyOldCode)
    BEGIN
    SET @vOldCode = (SELECT MyOldCode FROM Inserted)
    -- work out the new code
    EXECUTE MakeANewCode
    @OldCode = @vOldCode
    , @NewCode = @vNewCode OUTPUT
    INSERT INTO Inserted
    ( NewCode
    )
    VALUES ( @vNewCode
    )
    END
    END
    Sarah M

  2. #2
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You can't insert a row into INSERTED table, you have to UPDATE the base table with newcode value.

  3. #3
    Join Date
    Jun 2003
    Location
    UK
    Posts
    4
    thx a lot - I've got it now!

    I had assumed that it would be very similar to Interbase - not in this case!
    Sarah M

Posting Permissions

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