Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to run the script stored in a variable?

Posted on 2012-08-20
3
Medium Priority
?
487 Views
Last Modified: 2012-08-20
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
0
Comment
Question by:wilfordrocks
3 Comments
 
LVL 13

Accepted Solution

by:
LIONKING earned 2000 total points
ID: 38311638
You can use:

EXEC(@XSQL)
0
 
LVL 4

Expert Comment

by:fritzfrancis
ID: 38311649
Put parentheses around the variable.

EXECUTE (@XSQL)

You can also use the shortened form

EXEC (@XSQL)
0
 
LVL 9

Expert Comment

by:Vijaya Reddy Pinnapa Reddy
ID: 38311712
DECLARE @XSQL AS VARCHAR(8000)
SET  @XSQL = 'select * from tblTable where Tableid=921'
EXEC(@XSQL)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question