I have a piece of dynamic SQL I need to execute, I then need to store the result into a variable.
I know I can use sp_executesql
but can't find clear examples around about how to do this.
I have a piece of dynamic SQL I need to execute, I then need to store the result into a variable.
I know I can use sp_executesql
but can't find clear examples around about how to do this.
Return values are generally not used to "return" a result but to return success (0) or an error number (1-65K). The above all seem to indicate that sp_executesql does not return a value, which is not correct. sp_executesql will return 0 for success and any other number for failure.
In the below, @i will return 2727
SSMS will show this Msg 2727, Level 11, State 1, Line 1 Cannot find index 'NonExistantStaticsName'.
Declare @variable int
Exec @variable = proc_name
This worked for me:
If you want to return more than 1 value use this:
returned values are in @retIndex and @retText
DECLARE @ValueTable TABLE ( Value VARCHAR (100) )