Reterieving the result of dynamic query in a variable.

Puneet20884
Posted by Puneet20884 under Sql Server category on | Views : 1680
For this there are few tricks or the functionalities given by SQL.

1) Using a OUTPUT variable inside the dynamic query that sends the result of the dynamic query
2) Executing the query with sp_executesql function
3) Use of nVarchar type for cerating the dynamic query

The simple code goes as :

DECLARE @ValOut bigint

EXEC sp_executesql
N'SELECT TOP 1 @outPutValue = memberid FROM as_tblmembers order by 1 desc'
,N'@outPutValue bigint OUTPUT'
,@ValOut OUTPUT

SELECT @ValOut

Comments or Responses

Login to post response