Psst! Did you know DevelopmentNow is a mobile web site design agency?

Contact us for help mobilizing your site, or to sign up for our beta Mobile Web SDK!
all groups > sqlserver server > february 2006 >

sqlserver server : OUTPUT Clause SQL Server 2005


Uri Dimant
2/28/2006 12:00:00 AM
Hi
[quoted text, click to view]
UPDATE tbl_Proposal SET ProposalDesc = 'yu'
OUTPUT Deleted.ProposalDesc INTO @OldProposal
WHERE ProposalID=9;

SELECT * FROM @OldProposal




[quoted text, click to view]

Uri Dimant
2/28/2006 12:00:00 AM
??
Have you ran my soultion?
UPDATE tbl_Proposal SET ProposalDesc = 'yu'
OUTPUT Deleted.ProposalDesc INTO @OldProposal
WHERE ProposalID=9;

[quoted text, click to view]

Uri Dimant
2/28/2006 12:00:00 AM
I'm confused , try run this code and see

create table t ( i int not null );

create table table_audit ( old_i int not null, new_i int null );
insert into t (i) values( 1 );
insert into t (i) values( 2 );

update t
set i = i + 1
output deleted.i, inserted.i into table_audit
where i = 1;

delete from t
output deleted.i, NULL into table_audit
where i = 2;

select * from t;
select * from table_audit;

drop table t, table_audit;
go





[quoted text, click to view]

Uri Dimant
2/28/2006 12:00:00 AM
Adarsh
You don't need to put a WHERE condition twice. Does my example return a
right result, doesn't it?



[quoted text, click to view]

Tibor Karaszi
2/28/2006 12:00:00 AM
The OUTPUT clause doesn't have a WHERE clause. Check out the syntax in Books Online for the OUTPUT
clause:
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/41b9962c-0c71-4227-80a0-08fdc19f5fe4.htm

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
Dan Guzman
2/28/2006 12:00:00 AM
Adarsh, I think you are confused about the OUTPUT clause. Both the OUTPUT
clause and the WHERE clause are part of the same UPDATE statement. The
following will update only those rows with ProposalID=9, not all rows in the
table. Only the before image of the updated rows will be inserted into
@OldProposal.

UPDATE tbl_Proposal SET ProposalDesc = 'yu'
OUTPUT Deleted.ProposalDesc INTO @OldProposal
WHERE ProposalID=9;

--
Hope this helps.

Dan Guzman
SQL Server MVP

[quoted text, click to view]

Tibor Karaszi
2/28/2006 12:00:00 AM
The update has a WHERE clause. Just as usual. But the OUTPUT clause doesn't have a WHERE clause.
This is what it should look like:

UPDATE tblname
OUTPUT ...
WHERE...

The WHERE clause above belong to the UPDATE statement.

--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/


[quoted text, click to view]
Adarsh
2/28/2006 3:33:25 AM
Hi,

When I add WHERE CLAUSE IN UPDATE query the OUTPUT Clause doesn't work.


This code works :
DECLARE @OldProposal TABLE
(ProposalDesc varchar(200))


UPDATE tbl_Proposal SET ProposalDesc = 'yu'
OUTPUT Deleted.ProposalDesc INTO @OldProposal
SELECT * FROM @OldProposal


and this code doesn't work:


DECLARE @OldProposal TABLE
(ProposalDesc varchar(200))


UPDATE tbl_Proposal SET ProposalDesc = 'yu' WHERE ProposalID=9;
OUTPUT Deleted.ProposalDesc INTO @OldProposal WHERE ProposalID=9;
SELECT * FROM @OldProposal

The difference is only "WHERE ProposalID=9;" at the end of update query
and OUTPUT query
Adarsh
2/28/2006 3:45:55 AM
??
Adarsh
2/28/2006 3:59:54 AM
Yes this works but my update query is having WHERE Clause...

Thanks Uri.
Adarsh
2/28/2006 4:18:29 AM
Yes this works...

But please try running this:

create table t ( i int not null );


create table table_audit ( old_i int not null, new_i int null );
insert into t (i) values( 1 );
insert into t (i) values( 2 );


update t
set i = i + 1 where i = 1
output deleted.i, inserted.i into table_audit
where i = 1;


delete from t
output deleted.i, NULL into table_audit
where i = 2;


select * from t;
select * from table_audit;


drop table t, table_audit;
go



Note: I have added "where i = 1" in update query
Adarsh
2/28/2006 5:10:15 AM
Yes but I want to update the row only which has i = 1. What can I do in
that situation .
Becoz without Where Clause it will update all the records in t table.
Adarsh
2/28/2006 5:48:20 AM
#$%&*(...... Oh ok..... I got it now..;)
Thanks a lot Dan, Tibor and Uri....
AddThis Social Bookmark Button