bind variables dynamically in sql server

Viewed 23

I am running the below block in sql server 2014 , but it is getting failed with the error -

Must declare the table variable @tname.

Any ideas to fix this error?

DECLARE @IntVariable NVARCHAR(500);  
DECLARE @SQLString NVARCHAR(500);  
DECLARE @ParmDefinition NVARCHAR(500);  
  
/* Build the SQL string one time.*/  
SET @SQLString =  
     N'SELECT * into #t from @tname';  
SET @ParmDefinition = N'@tname NVARCHAR(500)';  
/* Execute the string with the first parameter value. */  
SET @IntVariable = 'tst_tbl';  
EXECUTE sp_executesql @SQLString, @ParmDefinition,  
                      @tname = @IntVariable;
0 Answers
Related