Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Using OUTPUT on Update

Author  Topic 

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 10:27:22
Hello,
I have the following update query:

UPDATE TableA
SET COL1 = i.Col1,
COL2 = i.Col2
FROM TableB o
JOIN TableC i
ON o.ID = i.ID

How can I use OUTPUT statement with this UPDATE query to get the updated column values.

Thanks.

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-06 : 10:37:59
[code]
UPDATE TableA
SET COL1 = i.Col1,
COL2 = i.Col2
OUTPUT INSERTED.col1, INSERTED.col2

FROM TableB o
JOIN TableC i
ON o.ID = i.ID
[/code]
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 14:54:28
This is not working because these data is not inserted, but it is updated. Any solution to this?

Thanks.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-06 : 15:08:38
If it is not working it is not because it is an update statement, it is because of something else. Are you getting an error message, or getting no data at all, or is it showing the wrong data?

Logically, update is a set of two operations - a delete and an insert. So you have both inserted and deleted virtual tables available to you.
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 15:57:38
I do not get any error message, but I do not get any values. I will double-check and let you know.

Thanks for your time.
Go to Top of Page

rama108
Posting Yak Master

115 Posts

Posted - 2013-03-06 : 16:28:32
The OUTPUT worked. Thanks.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-03-06 : 17:03:20
You are welcome - glad to help.
Go to Top of Page
   

- Advertisement -