Saturday, February 25, 2012

Capturing exec or sp_executesql result

Hi.. perhaps it's stupid, but i am really a newbie..
How can i capture the result generated from exec('select ... ') or sp_executesql? such as

sp_executesql 'SELECT max(ID) FROM Yr' + year(getdate) + '.dbo.Sales' + month(getdate)

How can i get the 'max(ID)' data? I must get the data from sp_executesql or execute form.as far as I know the only way to capture the results from EXECUTE is to insert it into a temp table and then select out what you are looking for.|||You can set a variable from sp_executesql

declare @.id int, @.sql nvarchar(1000)
select @.sql = 'SELECT max(ID) FROM Yr' + year(getdate) + '.dbo.Sales' + month(getdate)

exec sp_executesql @.sql, N'@.id int output', @.id output|||oops

declare @.id int, @.sql nvarchar(1000)
select @.sql = 'SELECT @.id = max(ID) FROM Yr' + year(getdate) + '.dbo.Sales' + month(getdate)

exec sp_executesql @.sql, N'@.id int output', @.id output

see
www.nigelrivett.com
sp_executesql

No comments:

Post a Comment