SQL Udate then Insert row into another table based on update row primary keys

Hi there,

I am trying to find out how I can insert rows into a table based on another table being updated. I am not interested in triggers as this is a manual process.

So what I would like is to retrieve the primary keys of the update rows in the table being updated then insert a new row in another table whilst using the returned values as a field value for the table being inserting into.

UPDATE tblA SET field1="Test" WHERE id=1 ANd id =2;
INSERT INTO tblB (field2) 
("Getprimary key values from tblA above")

Open in new window


Thank you for your help on this matter
databarracksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
How can id=1 AND id=2?  Please stop trying to dumb it down to the point of obscurity and just provide the table schema definitions.  How are you going to determine what has been updated?  A better option would be to look into the OUTPUT clause to make your inserts.

UPDATE tbla
SET ...
OUTPUT inserted.columnA, inserted.columnB, ... INTO tblB
0
databarracksAuthor Commented:
Here you go:

UPDATE tblA SET client_status = s.status
FROM tblA INNER JOIN
(SELECT t1.id, t1.status, t1.reason, dateadd(s,t1.timestamp_conf,'19700101 00:00:00:000') as time_confirmed
FROM OPENQUERY
(
   LinkedServerName,
   'SELECT * FROM incoming_db.incoming_message'
) AS t1 INNER JOIN tblA t2 ON t2.id = t1.id
WHERE CASE WHEN t1.status IS NOT NULL) as s ON tblA.id = s.id

Open in new window


Hope this makes it clearer. So this my update statement and from this somehow use your OUTPUT bit to insert the id value from the update statement into another table?
0
Brian CroweDatabase AdministratorCommented:
That helps a lot, try this...

UPDATE tblA
SET client_status = s.status
OUTPUT inserted.id
INTO tblB.Field2
FROM tblA AS A
INNER JOIN
(
	SELECT t1.id, t1.status, t1.reason, dateadd(s,t1.timestamp_conf,'19700101 00:00:00:000') as time_confirmed
	FROM OPENQUERY
	(
	   LinkedServerName, 'SELECT * FROM incoming_db.incoming_message'
	) AS t1
	INNER JOIN tblA AS t2
	ON t2.id = t1.id
	WHERE CASE WHEN t1.status IS NOT NULL
) as s ON tblA.id = s.id

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
When you use a join in an UPDATE, you should alias the table being updated and UPDATE the alias, not the original table name.  Otherwise mismatch errors can occur on the row that gets updated.  [The potential for that UPDATE anomaly is one reason that this capability is a MS extension, not part of ANSI SQL.]

Also, you should reduce the rows sent from the OPENQUERY if you can, so add the NOT NULL check to there as well.

Finally, the join to tblA in the derived table query seems unnecessary, as you join to tblA to do the UPDATE itself anyway.

Maybe try this:

UPDATE a
SET client_status = s.status
OUTPUT inserted.id INTO dbo.tblB ( field2 )
FROM dbo.tblA a
INNER JOIN (
    SELECT t1.id, t1.status, t1.reason, dateadd(second,t1.timestamp_conf,'19700101 00:00:00:000') as time_confirmed
    FROM OPENQUERY
    (
       LinkedServerName,
       'SELECT * FROM incoming_db.incoming_message WHERE status IS NOT NULL'
    ) AS t1
    WHERE t1.status IS NOT NULL --just to be sure, doesn't hurt
) as s ON a.id = s.id
0
databarracksAuthor Commented:
Excellent Brian worked perfectly.

Thank you so much  again for everything you have been most helpful
0
databarracksAuthor Commented:
Excellent work from Brian and Scott, both very good answers
0
databarracksAuthor Commented:
Thanks Scott I only saw your answer now and I appreciate your solution too and will apply that logic to my queries from now on
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.