Not able to delete a temp table

I have sql that will check if a temp table exists. If the table does exist I need to delete it(Which I suppose means to drop it), because immediately after deleting it, I want to recreate it. The code is not working, as I keep getting an error message: "There is already an object named #TempTable in the database. Can someone tell me what is wrong with the code?

IF object_id('#TempTable') is not null
BEGIN
  DROP TABLE #TempTable
END

CREATE TABLE #TempTable
(Id varchar(50),
 NAME varchar(50))
LVL 2
brgdotnetcontractorAsked:
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.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:
IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL
    DROP TABLE #TempTable

CREATE TABLE #TempTable
(Id varchar(50),
 NAME varchar(50))

Open in new window

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
Mike EghtebasDatabase and Application DeveloperCommented:
IF object_id('tempdb.dbo.#TempTable', 'U') is not null
  DROP TABLE #TempTable

Open in new window

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 2005

From novice to tech pro — start learning today.