Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

after insert update from another table Expand / Collapse
Author
Message
Posted Monday, February 18, 2013 5:05 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 19, 2013 9:29 AM
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.
Post #1421381
Posted Monday, February 18, 2013 6:59 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 12:08 PM
Points: 5,469, Visits: 23,460
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
Post #1421403
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse