Link to home
Start Free TrialLog in
Avatar of wilfordrocks
wilfordrocks

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Member_2_861731
Member_2_861731
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Put parentheses around the variable.

EXECUTE (@XSQL)

You can also use the shortened form

EXEC (@XSQL)
DECLARE @XSQL AS VARCHAR(8000)
SET  @XSQL = 'select * from tblTable where Tableid=921'
EXEC(@XSQL)