How to run the script stored in a variable?

DECLARE @XSQL AS VARCHAR(8000)
--assume the variable @XSQL was loaded from a select statement.
--How can I execute it?
SET  @XSQL = 'select * from tblTable where Tableid=921'
EXECUTE or GO @XSQL
wilfordrocksAsked:
Who is Participating?
 
LIONKINGCommented:
You can use:

EXEC(@XSQL)
0
 
fritzfrancisCommented:
Put parentheses around the variable.

EXECUTE (@XSQL)

You can also use the shortened form

EXEC (@XSQL)
0
 
Vijaya Reddy Pinnapa ReddyCommented:
DECLARE @XSQL AS VARCHAR(8000)
SET  @XSQL = 'select * from tblTable where Tableid=921'
EXEC(@XSQL)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.