Solved

SQL - If statement with temp table

Posted on 2014-02-12
9
435 Views
Last Modified: 2014-02-17
IF @ID =1
 BEGIN
	select field1
	INTO #tmpCurrent
	FROM TABLE1
 END
ELSE IF @ID = 2
 BEGIN
	
	select field1, field2
	INTO #tmpCurrent
	FROM TABLE2
 END

Open in new window

this gives me an error

There is already an object named '#tmpCurrent' in the database.
0
Comment
Question by:Ess Kay
9 Comments
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 250 total points
Comment Utility
Hello,

This is not possible since the compiler thinks that the table is already created in first condition. Either use different table names, OR create the table before the IF block and insert data into the table in your current blocks.

-Harish
0
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 250 total points
Comment Utility
Yes, this wont work with the temporarily, its a suprise though.... it works with the permanent tables.


you can achieve the same results by using a permemnant as below

IF OBJECT_ID('tmpCurrent') IS NOT NULL
    DROP TABLE  tmpCurrent

declare @T TABLE 
(
A INT, B INT
)

DECLARE @ID INT 
SET @ID = 1
IF @ID =1
 BEGIN

	select A
	INTO tmpCurrent
	FROM @T
 END
ELSE IF @ID = 2
 BEGIN
	select A,B
	INTO tmpCurrent
	FROM @T
 END

SELECT * INTO #tmpCurrent FROM tmpCurrent
drop table tmpCurrent

Open in new window

0
 
LVL 10

Expert Comment

by:PadawanDBA
Comment Utility
a little more context would be helpful...  Is this in a loop or no ?  If not, you could include something like:

if( object_id( N'tempdb..#tmpCurrent' ) is not null ) drop table #tmpCurrent;

Open in new window


before the select into.  Or you could actually create the temp table outside of the if statement (with the two columns) and then use an insert into inside of the if statement.
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Please try this:

IF @ID =1
 BEGIN
    begin try
    drop table #tmpcurrent
    end try
    begin catch
    end catch

      select field1
      INTO #tmpCurrent
      FROM TABLE1
 END
ELSE IF @ID = 2
 BEGIN
    begin try
    drop table #tmpcurrent
    end try
    begin catch
    end catch      
     select field1, field2
      INTO #tmpCurrent
      FROM TABLE2
 END

[Of course using a permanent table name instead is not the same thing, since multiple people running the code at the same time would destroy each others' tables.]
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Accepted Solution

by:
Ess Kay earned 0 total points
Comment Utility
thanks guys i solved it

Apparently you can't do it, the compiler check stops it
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
You should look at this q again.  You can do it, and I posted code that actually does it :-)
0
 
LVL 15

Author Comment

by:Ess Kay
Comment Utility
nope, i tried something similar to no avail scott, if it had worked, this question qouldnt be here
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Interesting ... I ran that code and it worked fine for me.

Are you talking about the code with the BEGIN TRY ... END CATCH construct?
0
 
LVL 15

Author Closing Comment

by:Ess Kay
Comment Utility
basically, i took this code
IF @ID =1
 BEGIN
	select field1
	INTO #tmpCurrent
	FROM TABLE1
 END
ELSE IF @ID = 2
 BEGIN
	
	select field1, field2
	INTO #tmpCurrent
	FROM TABLE2
 END


Select * from #tmpCurrent

Open in new window






and changed to


IF @ID =1
 BEGIN
	select field1
	INTO #tmpCurrent
	FROM TABLE1
Select * from #tmpCurrent
 END
ELSE IF @ID = 2
 BEGIN
	
	select field1, field2
	INTO #tmpCurrent2
	FROM TABLE2
Select * from #tmpCurrent2
 END

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now