i have problem declaring sql statement (MS SQL) database as variable i have this procedure , anyone can help thanks :)
DECLARE @ID int = 700001158
DECLARE @STRING VARCHAR(250) = 'StringResource_EN'
SELECT string.[value],item.[id] FROM ItemResource item LEFT JOIN **@STRING** string ON item.[name_id] = string.[code]
WHERE item.[id] = @ID
i got Error:
[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Must declare the table variable "@STRING". (1087) Blockquote
while it should be this result :
id = 700001158 value = Devildom Purge: Shield
Edited : i'v tried
DECLARE @ID int = 700001158
DECLARE @STRING VARCHAR(250) = 'StringResource_EN'
DECLARE @sql NVARCHAR(1000)
SET @sql = 'SELECT string.[value],item.[id] FROM ItemResource item LEFT JOIN '+@STRING+' string ON item.[name_id] = string.[code]
WHERE item.[id] = @ID'
EXEC sp_executesql @sql;
ALSO I got this
[42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Must declare the scalar variable "@ID". (137)
@IN_STRING_DBis meant to be a dynamic object, there's no reason to declare it as annvarchar(255). The longest length an object's name can have is 128 characters, so usenvarchar(128)orsysname.