When you execute a dynamic string with EXEC(), it executes in it's own context so any local temporary tables you create are destroyed once EXEC is finished. The trick here is to insert into a global temporary table, then you'll have access to the data after EXEC is finished. To avoid other sessions from accessing the global table (it could happen if you have concurrent sessions running), wrap the call to EXEC inside of a transaction and copy the data right away into a local temp table so other session can create the global table if needed. Here is a sample:
--------------
use Northwind
go
SET NOCOUNT ON
BEGIN TRANSACTION
EXEC('SELECT * INTO ##tmpGlobal FROM Products')
SELECT * INTO #tmpLocal FROM ##tmpGlobal
DROP TABLE ##tmpGlobal
COMMIT TRANSACTION
SELECT * FROM #tmpLocal
DROP TABLE #tmpLocal
---------------------
Roman
>Arnaldo...
>
>Thanks for responding. Unfortunately, that doesn't seem to work...once I do an EXEC (@cSQLCmd), the temp file that I wanted to direct the query to is immediately destroyed. I guess it's because SQL Server is calling the sp_execute function, which {I guess) erases any temp files before returning.
>
>So I'm in a 'catch-22'...I'd like to be able to do a ...
>
>INSERT INTO #temp EXEC (@cSQLCmd)...
>
>but I need to build the structure for #temp to begin with. And again, that structure is variable.
>
>Kevin
Roman Rehak, MCSD, MCDBA, MCSA
Competitive Computing
354 Mountain View Drive
Colchester, VT 05446-5824
802-764-1729