1

In below dynamic sql I am trying to check whether temporary table (#Table) already exists if not then create using some other table.

But my problem is that irrespective what is the value in IF condtion, I am always getting error for select statement which is inside If condition

There is already an object named '#Table' in the database.

Below is the sql code.

declare @sqlstring varchar(max)

set @sqlstring=N'

select * into #Table from mst_country


if(object_id(''tempdb..#Table'') is null)
begin

    select * into #Table from mst_country_bkp

end'

exec(@sqlstring)

Can any one please let us know why it can be happening?

2
  • Why are you doing that in dynamc SQL anyway? Commented Oct 28, 2015 at 9:15
  • Actually my original query is much complex and bigger..It is just a small snippet to show what is the exact problem.. If you replace mst_country whith any of your database table it will give you error..so basically to simplify question I have used this query. Commented Oct 28, 2015 at 9:17

2 Answers 2

3

You need to drop the temp table once you are done with your query like this:

declare @sqlstring varchar(max)

set @sqlstring=N'
IF OBJECT_ID(''tempdb.dbo.#Table'', ''U'') IS NOT NULL
DROP TABLE #Table;  

select * into #Table from mst_country


if(object_id(''tempdb..#Table'') is null)
begin  
    select * into #Table from mst_country_bkp    
end'

exec(@sqlstring)

EDIT:

The issue is that you cannot have two SELECT INTO inside the same sql statement. This is a leftover from SQL 6.5 which did not have deferred name resolution. Try this:

declare @sqlstring varchar(max)

set @sqlstring=N'
IF OBJECT_ID(''tempdb.dbo.#Table'', ''U'') IS NOT NULL
DROP TABLE #Table;  

select * into #Table from mst_country

insert into #Table(col1,col2,....)
select col1,col2,.... from mst_country_bkp    
end'

exec(@sqlstring)
Sign up to request clarification or add additional context in comments.

16 Comments

Perhaps put an if exists...drop table at the start.
@SagarShirke At least try the code given to you before dismissing it so quickly.
With your last modification, you risk having data from both tables in the temporary table
@RahulTripathi...Thanks, your link has helped me to understand what is the problem..I appreciate your time and efforts for helping me..
Very Good @RahulTripathi +1. Nice article you found
|
2

When compiling the sqlserver doesn't allow you creating the same temporary table in the same scope twice. You can actually cheat the sqlserver like this, so it doesn't realize that the temporary table was already created:

declare @sqlstring varchar(max)

set @sqlstring=N'

exec(''select * into #Table from mst_country'')


if(object_id(''tempdb..#Table'') is null)
    select * into #Table from mst_country_bkp
'

exec(@sqlstring)

2 Comments

It worked perfectly fine..Thanks..But one query..You are saying " sqlserver doesn't allow you creating the same temporary table in the same scope twice" but I have used IF condition..so you want to say..sql server will not consider if statement?
That is correct. You are attempting to create the same table again. Even though you have an condition. SQL server doesn't evaluate your condition, the error occurs at compile time, not the execution itself

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.