logo       

Re: Using dynamic SQL to set a variable: msg#00100

Subject: Re: Using dynamic SQL to set a variable
I've always INSERT values from EXEC statements into a temp table, to
use it.  Eg. below (remove first two lines, and substitute @tblName
with @tableName in your SQL.:

DECLARE @tblName varchar(50)
SET @tblName = 'tblTemp'

CREATE TABLE #tblTest(intRowCount int)
INSERT INTO #tblTest
EXEC ('SELECT COUNT(*) FROM ' + @tblName)

SELECT * FROM #tblTest

DROP TABLE #tblTest


George

************************************************
On Fri, 24 Dec 2004 16:37:56 -0000, Keith R. Pinster
<keith.pinster@xxxxxxxxx> wrote:
> 
> 
> This is too weird.  I'm trying to loop through my tables to get row
> counts.  I have a cursor which runs through the tables fine.  In the
> loop, I execute this statement:
> 
> SET @rowCount = EXEC ('SELECT COUNT(*) FROM ' + @tableName)
> 
> and I get an error saying that there is Incorrect syntax near the
> keyword 'EXEC'.  However, if I just run the statement:
> 
> EXEC ('SELECT COUNT(*) FROM ' + @tableName)
> 
> it works fine.  Can't I set a variable using dynamic SQL this way?  I
> was sure I had done it before...
> 
> KRP
> 
> 
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 


-- 
George Kanchanavaleerat


------------------------ Yahoo! Groups Sponsor --------------------~--> 
Make a clean sweep of pop-up ads. Yahoo! Companion Toolbar.
Now with Pop-Up Blocker. Get it for free!
http://us.click.yahoo.com/L5YrjA/eSIIAA/yQLSAA/dpFolB/TM
--------------------------------------------------------------------~-> 

 


<Prev in Thread] Current Thread [Next in Thread>