?
Solved

SQL - If statement with temp table

Posted on 2014-02-12
9
Medium Priority
?
447 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 69

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 69

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 69

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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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
Suggested Courses

762 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