-3

I create a temp table. Then I run a With statement. I put the results in a temp table and then, after I use the data, I can not delete it because this error shows up: "There is already an object named '#ArmadoPlantilla' in the database."

What am I doing wrong?

Max
  • 199
  • 1
  • 3
  • 11

2 Answers2

1

Make sure you drop your temp table after you are done with it. Sounds like you created your temp table, used it and then left the stored proc (or whatever) without dropping it. I typically use this method (though @Giscard's may work):

CREATE TABLE #ArmandoPlantilla(
whatever int,
whateverAgain char(30) )

--sql inserting records and doign stuff with the temp table

drop table #ArmandoPlantilla  -- **HERE** Are you missing this?

Also... post your freaking code. Are you trying to make this hard for us?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
0

Please post your code. The error message you mention indicates you are trying to create the table more than once. Are you using select ... into #ArmandoPlantilla... to insert into the temp table after the table has already been created?

Here's a sample of how you can ensure that your temp table is dropped and recreated at the start of your script. You should be able to modify to only create if the temp table doesn't exist yet:

if object_id('tempdb..#ArmandoPlantilla') is not null begin drop table #ArmandoPlantilla end
go
create table #ArmandoPlantilla (id int not null)
go
with f as (
    select 1 as [id]
    union select 2
)
insert #ArmandoPlantilla
    select * from f
go
select * from #ArmandoPlantilla
Giscard Biamby
  • 4,569
  • 1
  • 22
  • 24
  • Yeah, I try to delete the temp table at the end of the procedure, but it returns that error. Can you be more specific about the 'temp..#armadoplantilla'..? where can I find the temp table to make it work? – Max Feb 17 '12 at 17:59
  • 2
    @Max - Why don't you show your code?, you would get an answer to your problem in a couple of minutes once you do – Lamak Feb 17 '12 at 18:04
  • I thought you would know where the #armadoplantilla temp table is coming from, because you posted it in your question. If you want more help you're going to have to post code. – Giscard Biamby Feb 17 '12 at 18:11