Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Temporary table disappears!

Author  Topic 

truthseeker
Starting Member

15 Posts

Posted - 2010-09-27 : 14:04:31
Currently, I'm using a stored procedure that imports an excel file to sql server like so:


DECLARE @sqlimport nvarchar(1000)
DECLARE @filename nvarchar(100)

SET @filename = 'D:\test.xls'


SET @sqlimport = '
SELECT * INTO #Test1 FROM OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database='+@filename+';HDR=NO,IMEX=1'',
''SELECT * FROM [Sheet1$]'')
'

EXEC (@sqlimport)


This returns the result and I can see that it returned:

(809 row(s) affected)

However when I select it immediately right after the exec command:

SELECT * FROM #Test1

I get
Msg 208, Level 16, State 0, Line 15
Invalid object name '#Test1'.


What's happening and how do I solve this?

Thanks!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-09-27 : 14:06:31
Because your query isn't in the same session. Dynamic SQL runs in a different session.

You'll need to either use a global temp table or put your query into the dynamic sesson.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

truthseeker
Starting Member

15 Posts

Posted - 2010-09-27 : 14:52:42
Thanks! That helped a lot!

I decided to use a global temp table and it works great!
Go to Top of Page
   

- Advertisement -