By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,842 Members | 2,368 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,842 IT Pros & Developers. It's quick & easy.

Problem placing results of generated select statement into dynamic temp table

P: 3
How can I place the results of a dynamically generated select statement into a (#) temp table?

This has me stumped, any help would be greatly appreciated.

(The names used in the following aren't the actual names, just using the name of what that position represents for readability)

The following query is what I want to use, which works as desired:

SELECT * INTO #TMP FROM [LinkedServer].[DBName].Schema.Table EXCEPT
SELECT * FROM Schema.Table

However, the Schema/Table wont be static (including not knowing the # of columns in the table). I'm trying to place this into a SP where I pass the Schema name and Table name as parameters so I can construct something like the following:

DECLARE @TableName varchar(100)
SET @TableName = 'MyTable'
DECLARE @SchemaName varchar(100)
SET @SchemaName = 'MySchema'

DECLARE @SQL varchar(100)

SET @SQL = ('SELECT * INTO #TMP FROM [LinkedServer].[DBName].' + @SchemaName + '.' + @TableName + ' EXCEPT SELECT * FROM ' + @TableName)

EXEC (@SQL)

Then the results would be placed into the temporary table #TMP.

The above reports an error that #TMP doesn't exist. I can't manually create the temp table as the number of columns vary for each passed table.

Again it works as desired as long as I supply it with the actual values.

The results are being placed into a CURSOR so I can iterate thru each result.

Surely there has to be a way I can do this.

Joey
Oct 4 '08 #1
Share this Question
Share on Google+
3 Replies


ck9663
Expert 2.5K+
P: 2,878
It has something to do with the instance of temp table. It is only visible within the same instance or any instance created within. An EXEC creates a separate so the #temp table created inside the EXEC is not visible outside.

Create your temp table before the EXEC and use INSERT INTO instead of SELECT INTO

Happy coding.

-- CK
Oct 5 '08 #2

P: 3
It has something to do with the instance of temp table. It is only visible within the same instance or any instance created within. An EXEC creates a separate so the #temp table created inside the EXEC is not visible outside.

Create your temp table before the EXEC and use INSERT INTO instead of SELECT INTO

Happy coding.

-- CK
Thanks for the response.

Is there a way I can create a global temp table w/o having to provide it with fields?

I can use: SELECT * INTO ##TMP FROM SomeTable

Then I have a global temp table with all the columns from that table.

I'm passing the table name into this procedure so the select statement has to be dynamic. 'SELECT * INTO ##TMP FROM ' + @SomeTableName

And it says invalid object name '##TMP'.
Oct 5 '08 #3

P: 3
I was able to accomplish what I needed and thought I would share it in case someone else was curious:

declare @Result varchar(200)
declare @TableName varchar(100)
set @TableName = 'My_TestTable'

declare @SQLCommand varchar(200)
set @SQLCommand = ('SELECT * FROM ' + @TableName + ' EXCEPT SELECT * FROM ' + @TableName)

/* Create link to self */
exec sp_addlinkedserver N'LocalSQL', ' ', N'SQLNCLI', N'.\SQLEXPRESS', '','', N'DBNameHere'

declare @EXECString varchar(300)
set @EXECString = ('SELECT * INTO #TMP FROM OPENQUERY(' + LocalSQL + ',''' + @SQLCommand + ''')')

exec(@EXECString)

declare @ChangedRecords CURSOR
SET @ChangedRecords = CURSOR FOR
SELECT Field1 FROM #TMP

open @ChangedRecords
FETCH NEXT FROM @ChangedRecords INTO @Result

while 1=1
begin
if @@FETCH_STATUS <> 0
break

begin
exec('DELETE FROM ' + @TableName + ' WHERE Field1 = ' + @Result)
end

FETCH NEXT FROM @ChangedRecords INTO @Result
end
close @ChangedRecords
deallocate @ChangedRecords
go
drop table #tmp
Oct 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.