I have the code below, where I want to get the id of a row via dynamic SQL.
Please note that variables @posselect
and @catselect
are filled, but I left that out for overview.
declare @SQL nvarchar(1000)
declare @posselect nvarchar(50)
declare @catselect nvarchar(100)
declare @bannerid int
SET @SQL = 'SELECT TOP 1 @id = id FROM banners
WHERE publishdate < GETDATE()
AND depublishdate > GETDATE()' + @posselect + @catselect +
'ORDER BY [views] ASC'
EXEC sp_executesql @SQL, @bannerid = @id output 'on this line I get the error
print '@bannerid:'+STR(@bannerid)
I get the error:
Must declare the scalar variable "@id".