Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


after insert update from another table


after insert update from another table

Author
Message
cts_account
cts_account
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 7
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.
bitbucket-25253
bitbucket-25253
SSCertifiable
SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)SSCertifiable (5.7K reputation)

Group: General Forum Members
Points: 5695 Visits: 25280
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

Before posting a performance problem please read
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search