OUTPUT command in MSSQL 2005 Print E-mail
Written by Stanislav Duben   
Wednesday, 29 August 2007
OUTPUT Command in MSSQL 2005In MSSQL 2005 is new T-SQL command OUTPUT, that was used only as output parameters in previous MSSQL version. Now you can use it in T-SQL commands to make data changes and see results by one command without using Triggers.






When you inserting new row, OUTPUT can returns you inserted record includes identity ID columns. For get ID value you must used Scope_Identity(), or get last value after insert. This is very easy now. 

Example:
INSERT INTO Companies (CompanyName, WebSite)
OUTPUT inserted.ID, inserted.CompanyName
VALUES ('Select SQL', 'www.select-sql.com')

This will return you:
ID          CompanyName
----------- ----------------------
3           Select SQL
(1 row(s) affected)


In update command you can use inserted or deleted. You can use both in one query. It shows you data after update (inserted table) and before update (deleted table).

Example:
UPDATE Companies SET CompanyName = 'New name'
OUTPUT inserted.CompanyName, deleted.CompanyName, inserted.ID
WHERE ID = 3

Query return:
NewCompanyName     OldCompanyName       ID
------------------ -------------------- -----------
New name           Select SQL           3
(1 row(s) affected)


Same way you can use OUTPUT in DELETE command, to show deleted record.

Example:

DELETE FROM Companies
OUTPUT deleted.CompanyName, deleted.ID
WHERE ID = 2

Returns:

CompanyName                     ID
------------------------------- -----------
Select SQL                      2
(1 row(s) affected)
Tag it:
Delicious
Digg
YahooMyWeb
Technorati
 

Comments  

 
0 #5 Harish 2010-07-07 14:28
Quoting Christopher:
got one question.. ^^

what if i want to store the
result returned by the "OUTPUT",
and not just sending it to client..

ex is i want the OUTPUT result to
be inserted to a variable table?..

if you know how, please let me know..
thanks.. peace out... ^^

Quoting Christopher:
got one question.. ^^

what if i want to store the
result returned by the "OUTPUT",
and not just sending it to client..

ex is i want the OUTPUT result to
be inserted to a variable table?..

if you know how, please let me know..
thanks.. peace out... ^^


You can store the result set returned from the update command in table as shown below:

UPDATE Companies SET CompanyName = 'New name'
OUTPUT inserted.CompanyName, deleted.CompanyName, inserted.ID
into @table
WHERE ID = 3
Quote
 
 
0 #4 masmas 2009-08-05 12:56
your are beeest
Quote
 
 
0 #3 Stanislav Duben 2008-01-10 09:05
Hi Christopher, this is pretty simple. You can use syntax OUTPUT ... INTO ...

See my example here:

Code: <br />UPDATE test SET test='example test'<br />OUTPUT deleted.id, deleted.test INTO test2 (id, test2)<br />WHERE id<1022<br />
Quote
 
 
0 #2 Christopher 2008-01-09 02:00
got one question.. ^^

what if i want to store the
result returned by the "OUTPUT",
and not just sending it to client..

ex is i want the OUTPUT result to
be inserted to a variable table?..

if you know how, please let me know..
thanks.. peace out... ^^
Quote
 
 
0 #1 FAISAL 2007-09-26 06:24
i never know about this before, this a simple code but so informative

thanks a bunch Stanislav Duben!
Quote
 

Add comment


Security code
Refresh

< Prev   Next >