Anyone knows if it is possible to change an input variable during the execution of sp_executesql?
For example, i want to change @var value to ‘bye’
declare @var varchar(10) = 'hello'
declare @Query nvarchar(max) = 'print @var set @var = ''bye'' print @var'
EXEC SP_EXECUTESQL @Query, N'@var varchar(10)', @var -- Execute Query Statement
print @var
The last time I do print @var, I was expecting that it prints ‘bye’, but printed ‘hello’.
Can achive my aim without declaring output variables?
Thanks in advance
You’d have to use an
outputparameter instead:Note that the output is both in the variable declaration, and the actual parameter to
sp_executesql.