June 21, 2007 at 2:04 pm
The following script is NOT allowing me to SELECT * FROM #temp_Users -- (last statement below)
Any idea how I can dynamically create a #temp table then select from it? (In this scenario we prefer NOT to create a permanent table in the DB and subsequently drop it)
IF object_id('#temp_Users') is not null
DROP TABLE [#temp_Users]
GO
DECLARE @sql VARCHAR(1000)
SET @sql = 'select * INTO #temp_Users FROM MyDatabase..Users '
EXEC(@SQL)
--select * INTO #temp_Users FROM MyDatabase..Users
SELECT * FROM #temp_Users
June 21, 2007 at 2:25 pm
The table is created in the exec statement. It is automatically destroyed when that connection is dropped (before the execution goes back to the procedure).
You'll have to create that table before the EXEC SQL ('select * INTO #temp_Users FROM MyDatabase..Users WHERE 1 = 0)
Then in the exec : INSERT INTO #temp_Users (Columns) SELECT ...
Then you'll have access to the data in the master procedure.
June 21, 2007 at 2:50 pm
Ninja's right... but going beyond that there are some other issues here:
When you check the existence of a temporary table using object_id(), you have to reference it in tempdb... so change your IF statement to: IF object_id('tempdb..#temp_users') is not null...
There's also no reason to use dynamic sql in this example. Unless you're simplifying it for posting, just use his example without the EXEC()... In my experience Dynamic sql can be a useful tool but is kind of a nasty headache a lot of the time when it's not necessary.
June 21, 2007 at 3:50 pm
Ya I'm kind of sick of that don't use dynamic sql discussion. If they want to hang themselves, I let 'em. I'm not here to save the world anymore. But your point, that point is still very valid.
I was assuming here that the code was something around a dynamic where clause. That's also why I didn't mention it. But if the presented code is the actual code, there's absolutely no need for dynamic sql. There's even no need for the temp table... Need I go on?
June 21, 2007 at 11:31 pm
I agree with Aaron and Remi... there is no need for any dynamic SQL here. Also, you must follow Aaron's suggestion for how to test for the presence of a temp table.
--Jeff Moden
Change is inevitable... Change for the better is not.
June 25, 2007 at 1:20 pm
The best way to check for a temp table (SQL 2005 only) is to also include the object type parameter in the OBJECT_ID function call. This makes sure that it is a table, and not some other type of temporary object.
if object_id('tempdb..#temp_users','u') is not null print 'table #temp_users exists' else print 'table #temp_users does not exist'
For SQL 2000 and before, you can do it this way:
if exists ( select * from tempdb.dbo.sysobjects o where o.xtype in ('U') and o.id = object_id( N'tempdb..#temp_users' ) ) print '#temp_users exists' else print '#temp_users does not exist'
June 25, 2007 at 1:30 pm
Thanks for the tip, this may avoid some small gotchas that can make you go mad. The object is a very nice touch on this one.
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply