3

I have the next script example:

DECLARE @lCode INT
DECLARE @lText VARCHAR(400)
DECLARE @lConditionName VARCHAR(40)
DECLARE @Aux VARCHAR(400)

SET @lCode = 1234;
SET @lConditionName = 'Code'

SET @Aux = '@l' + @lConditionName

SET @lText = 'SELECT * FROM Table WHERE Code = ' + @Aux

PRINT @lText

The result is:

SELECT * FROM Table WHERE Code = @lCode

How can i obtein the value of @Aux Var like this?

SELECT * FROM Table WHERE Code = 1234
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43

2 Answers2

3
DECLARE @lCode INT
DECLARE @lText NVARCHAR(400)
DECLARE @lConditionName NVARCHAR(40)
DECLARE @Aux NVARCHAR(400)

SET @lCode = 1234;
SET @lConditionName = N'Code'

SET @Aux = N'@l' + @lConditionName

SET @lText = N'SELECT * FROM Table WHERE Code = ' + @Aux

PRINT @lText

EXEC  sp_executesql @lText, N'@lcode INT', @lcode
Quassnoi
  • 413,100
  • 91
  • 616
  • 614
0

You can try this :

DECLARE @lCode INT
DECLARE @lText NVARCHAR(400)
DECLARE @lConditionName NVARCHAR(40)
DECLARE @Aux NVARCHAR(400)

SET @lCode = 1234;
SET @lConditionName = N'Code'

SET @Aux = N'@l' + @lConditionName

SET @lText = N'SELECT * FROM Table WHERE Code = ' + @Aux

set @lText = N'Print ' + @Aux
print @lText
EXEC  sp_executesql @lText, N'@lcode INT', @lcode

Output

Print @lCode
1234
Md. Suman Kabir
  • 5,243
  • 5
  • 25
  • 43