Solved

SQL - If statement with temp table

Posted on 2014-02-12
9
444 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 250 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 250 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
connection to SQL 2012 error in windows 10 18 45
SQL XPCMDSHELL SQLCMD 1 38
SQL Convert rows to columns 5 32
Restore a log backup compressed 3 16
In this article I will describe the Copy Database Wizard 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.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

739 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