after insert update from another table

  • newbie here...can't seem to get this to function properly, tried creating many variants of standalone triggers and triggers calling a stored procedure passing variables. During testing, all of the records in one table get updated instead of only one. Here's my scenario in MSSQL2008:

    table1 with 5 columns, column1 is primary key int, col(2,3,4 & 5) are varchar(25)

    table2 has the exact same columns

    when a record is inserted into table1,

    1)lookup the matching record in table2 where the datavalue in table1.column2 = datavalue in table2.column2

    2)then update columns 3, 4 & 5 in the inserted record (table1) with the data from result set from their matching columns in table2, ie. where table1.3=table2.3, table1.4=table2.4, table1.5=table2.5;

    3)only use the newest record in table2

    4)only update the newly inserted record

    Any help would be great, thanks.

  • Believe what you may be looking for is the MERGE statement. For a discussion and example of how it works:

    http://www.simple-talk.com/sql/learn-sql-server/the-merge-statement-in-sql-server-2008/

    If you would/could post the table definition, some sample data and desired result someone may be able to assist you with tested sample T-SQL.

    To provide the sample as I suggested please click on the first link in my signature block and read the article. The article has sample T-SQL to allow you to post the table definition, some sample data and desired result simply and easily.

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply