creating #temp table throwing error

Hi All,

I am trying to create a temp table based on condition. However i am receiving error. I have two temp tables named #temp1 and #temp2. This is basically to do a search.

I fetch all required data into #temp1 initially.  
if exists(select name from tempdb.sys.tables where [name] like '#Temp2%')
begin
drop table #temp2
end

if (searchterm is integer)
begin
select records from #temp1 into #temp2 for searchterm --simplified here , query is more complex
end

else

begin
if exists(select name from tempdb.sys.tables where [name] like '#Temp2%')
begin
drop table #temp2
end
select records from #temp1 into #temp2 for searchterm 
end

Open in new window


I am using a select into to create #temp2. I cannot change name #temp2. When i execute the above i receive a error as below

"There is already an object named #Temp" in the database".

Any help is appreciated.
JyozealAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
At line 1 and 14, remove % from  '#Temp2%')

use

if exists(select name from tempdb.sys.tables where [name] like '#Temp2')

or try:
if exists(select name from tempdb.sys.tables where [name] like '#Temp2' + '%')
begin
drop table #temp2
end

if (searchterm is integer)
begin
select records from #temp1 into #temp2 for searchterm --simplified here , query is more complex
end

else

begin
if exists(select name from tempdb.sys.tables where [name] like like '#Temp2' + '%')
begin
drop table #temp2
end
select records from #temp1 into #temp2 for searchterm 
end

Open in new window


or just:
if exists(select name from tempdb.sys.tables where [name] ] = [#Temp2])
begin
drop table #temp2
end

if (searchterm is integer)
begin
select records from #temp1 into #temp2 for searchterm --simplified here , query is more complex
end

else

begin
if exists(select name from tempdb.sys.tables where [name]  = [#Temp2])
begin
drop table #temp2
end
select records from #temp1 into #temp2 for searchterm 
end

Open in new window

0
JyozealAuthor Commented:
eghtebas, thanks for your reply. I tried above options still same error. When i do a select into it creates a new temp table . However when select into is encountered again in else is sql server assuming that this object might already exist?

since i am doing it in if else only one of them gets executed at point of time.
0
Mike EghtebasDatabase and Application DeveloperCommented:
How about using something like:

if (searchterm is integer)
begin
   if exists(select name from tempdb.sys.tables where [name] ] = [#Temp2])
     begin
        delete from #temp2
        Inset  #temp2 Select * From #temp1;
    end
 else
   select records from #temp1 into #temp2 for searchterm --simplified here , query is more complex
end

Open in new window


Provided the line below doesn't include an identity column.
Inset  #temp2 Select * From #temp1;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JyozealAuthor Commented:
I used CTE s and insert into to get around the issue. Thanks for all the help.
0
JyozealAuthor Commented:
I used CTE s and insert into to get around the issue
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.