Hi all,
In sql server What is the use of Output keyword ? Explain me with an example?
Thanks,
M.Maheswar
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Oct 7, 2011, 3:12 AM EDT
http://msdn.microsoft.com/en-us/library/ms177564.aspx
Prabhu RajaRank #861 · 2.6K reputationPosted Oct 8, 2011, 3:12 AM EDT
AartiRank #2,383 · 594 reputationPosted Nov 18, 2011, 2:18 AM EST
After performing data entry on a table or a view, to output the results in a separate table, you can use the OUTPUT operator. The formula to follow is:
INSERT INTO TableName
OUTPUT INSERTED.Columns
VALUES(Value_1, Value_2, Value_X)
Here is an example:
CREATE TABLE Videos
(
Title nvarchar(50),
Director nvarchar(50),
WideScreen bit,
Rating nchar(10),
YearReleased int
);
INSERT INTO Videos
OUTPUT inserted.*
VALUES(N'War of the Roses (The)', N'Dany de Vito', 0, N'R', 2001),
(N'Memoirs of a Geisha', N'Rob Marshall', 1, N'PG-13', 2006),
(N'Last Castle (The)', N'Rod Lurie', 1, N'', 2001),
(N'Sneakers', N'Phil Alden Robinson', 1, N'PG-13', 2003);
When this statement executes, if you are working in the Microsoft SQL Server Management Studio,
the lower part would display a list of the records that were added
Thanks.
Datta KharadRank #2,135 · 668 reputationPosted Nov 18, 2011, 2:55 AM EST
OUTPUT parameters when it comes to "named parameters" or "ordinal parameters". In SQL Server this terminology this applies to the EXEC call and how you specify parameters there: not direction
Ordinal = position must match and datatype must be compatible
Named = assign the local value to the stored proc parameter name. Only datatype must be compatible
This is ordinal
declare @bob as int, @rich as char(20)
--OK
exec test @bob output, @rich output
GO
declare @bob as int, @rich as char(20)
--Fail
exec test @bob output, @rich output
This is named
declare @bob as int, @rich as char(20)
--OK
exec test
@ID = @bob output,
@mark = @rich output
GO
declare @bob as int, @rich as char(20)
--OK
exec test
@mark = @rich output,
@ID = @bob output