Solved

IF NOT EXISTS and SELECT INTO

Posted on 2009-07-07
6
270 Views
Last Modified: 2012-05-07
How come when I have the following statement twice in one SQL batch, it gives me an error.  If I open an query analyzer and run them one at a time (maintaining an open connection to preserve temp table) it does not give me an error.

This code is simplified for this post - but there is an automated set of routines which generate the SQL scripts and so this TempTable could be created multiple times.  To avoid that, I want to check if it exists.  

Why will these not work when run together but will work fine when run separately (again in same connection so the second statement actually sees that the first created the table).

The error I get states:
There is already an object named '#TEMP_NAMEHERE' in the database.

Any help is appreciated.

IF NOT EXISTS (SELECT * FROM tempdb..sysobjects WHERE name LIKE '#TEMP_NAMEHERE____%') BEGIN SELECT [FIELD] AS KEYVALUE INTO [#TEMP_NAMEHERE] FROM Database..Table WHERE WhereClause END
 
 IF NOT EXISTS (SELECT * FROM tempdb..sysobjects WHERE name LIKE '#TEMP_NAMEHERE____%') BEGIN SELECT [FIELD] AS KEYVALUE INTO [#TEMP_NAMEHERE] FROM Database..Table WHERE WhereClause END

Open in new window

0
Comment
Question by:jport88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24800368
try this way
IF OBJECT_ID('tempdb..#TEMP_NAMEHERE') IS NOT NULL
  BEGIN SELECT [FIELD] AS KEYVALUE INTO [#TEMP_NAMEHERE] FROM Database..Table WHERE WhereClause END
0
 
LVL 2

Author Comment

by:jport88
ID: 24800406
Still had issue.  I think the issue may actually be that SQL Server does not allow thre same TEMP table to be created in a SQL batch more than once: http://www.informit.com/articles/article.aspx?p=25288&seqNum=4

Meaning I would need to not store it as a TEMP table.

Does that make sense?
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24800420
why dont you create the table first and insert data

IF OBJECT_ID('tempdb..#TEMP_NAMEHERE') IS NOT NULL
create table #Temp_NameHere (Field int  )
 insert INTO [#TEMP_NAMEHERE]
 SELECT [FIELD] FROM Database..Table WHERE WhereClause
0
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24800426
You can try dropping that #temp table if you want to create it several times right.

if exists ( select * from information_schema.tables where table_name = '#temp')
drop table #temp
go

Now include this wherever you need to create that temp table.

Hope this helps
0
 
LVL 15

Accepted Solution

by:
rob_farley earned 500 total points
ID: 24800528
Unfortunately there's a bug in the SQL parser, so that it won't realise you've just dropped the temporary table before you try to recreate it (when you save a stored procedure that drops an object just before populating it with SELECT ... INTO ...)

Best is to CREATE the temporary table at the start, use INSERT statements throughout, and then DROP it at the end.

Hope this helps,

Rob
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24800723
Ignore my earlier script..

Use this one to drop #temp table before it is created again:

if OBJECT_ID('tempdb..#test') is not NULL
DROP TABLE #temp
go
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question