SET @tab = 'tableName'
SET @field1 = (SELECT field1 FROM '+@tab+' WHERE colName IS NULL)
I'm getting this error:
Must declare the table variable "@tab".
I need to set the result on @field1
SET @tab = 'tableName'
SET @field1 = (SELECT field1 FROM '+@tab+' WHERE colName IS NULL)
I'm getting this error:
Must declare the table variable "@tab".
I need to set the result on @field1
 
    
    For a SQL Server database, use EXECUTE sp_executesql to run the dynamic sql you setup.  (Notice the correct placement of the quotes compared to your post).
SET @tab = 'tableName'
SET @field1 = 'SELECT field1 FROM ' + @tab + ' WHERE colName IS NULL'
EXECUTE sp_executesql @field1
However, to save the return value you should look into defining OUTPUT as shown in this answer:  https://stackoverflow.com/a/3840771/9392034
