all groups > sql server mseq > august 2005 >
You're in the

sql server mseq

group:

EXEC (select... ) problem Help!


Re: EXEC (select... ) problem Help! Hugo Kornelis
8/6/2005 12:00:00 AM
sql server mseq:
[quoted text, click to view]

Hi Scagnetti,

First: do try to solve this without dynamic SQL. There are often other
ways to get the same result. Consider posting your real problem here;
maybe someone sees a solution you didn't think of. For a discussion and
the pro's and (mainly) con's of dynamic SQL, visit Erland Sommarskog's
site: http://www.sommarskog.se/dynamic_sql.html.

The answer to your question is to use the stored procedure sp_executesql
instead of exec (@sql). Details in Books Online (and on Erland's page).
But as I said - doing it without dynamic SQL is the best solution.

Best, Hugo
--

EXEC (select... ) problem Help! Scagnetti
8/6/2005 8:20:03 AM
How can I get this to work?
declare @WkEmpID
declare @sql varchar(1000)

set @sql = 'select distinct @WkEmpID = EmpID from Employee'
exec (@sql)

Now this is a simplified version of a more comples query which is forcing me
to use this method rather than a simple SQL query. The big problem is getting
Re: EXEC (select... ) problem Help! Dandy Weyn [Dandyman]
8/9/2005 12:00:00 AM
Here's an example on how to build a dynamic SQL string and run it with
EXECUTE or SP_EXECUTESQL stored procedure

http://www.dandyman.net/sql/samples/dynamicsql.txt


--
Dandy Weyn
[MCSE-MCSA-MCDBA-MCDST-MCT]
http://www.dandyman.net

Check my SQL Server Resource Pages at http://www.dandyman.net/sql
[quoted text, click to view]

AddThis Social Bookmark Button