[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL - If statement with temp table

Posted on 2014-02-12
9
Medium Priority
?
450 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
[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
9 Comments
 
LVL 12

Assisted Solution

by:Harish Varghese
Harish Varghese earned 1000 total points
ID: 39854004
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 1000 total points
ID: 39854042
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
ID: 39854056
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39854259
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
 
LVL 15

Accepted Solution

by:
Ess Kay earned 0 total points
ID: 39854285
thanks guys i solved it

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

Expert Comment

by:Scott Pletcher
ID: 39854301
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
ID: 39854535
nope, i tried something similar to no avail scott, if it had worked, this question qouldnt be here
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39854870
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
ID: 39864324
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

649 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