We help IT Professionals succeed at work.
Get Started

Understanding and using temp tables properly..."There is already an object named '#tbl' in the db"

Last Modified: 2012-05-07
Hi everyone,

I'm working on a report, esp. the backend sql queries.  The main stored proc. contains a few other stored procedures.  The idea is to store the calculations into temp tables and finally putting them into the main temp table and outputting the data.

So I'm in the testing phase, and am testing stored proc "CalculateHoursAlreadyWorkedThisCycle".  Within this sp, a temp table called #temp_table (tricky! ha!) is created.  After I extract needed data from it, it inserts into another temp table called #tblACRWeekHours_temp. ( I will show code if needed).  #tblACRWeekHours_temp usually gets created in the main stored proc, but since I'm testing on my own, I manually create it.  Following will be code. When I run the following set of commands in a new query window in management studio, I get this error:
"Msg 2714, Level 16, State 6, Line 6
There is already an object named '#tblACRWeekHours_temp' in the database."

This is making testing unpredictable and hard.  I've tried to google this and found a posting or two, but I don't understand why it's happening.  I'm typically a .NEt programmer who has been thrown into the SQL shark infested waters.  So, yeah, help!  TIA.
/* Shouldn't following 4 lines wipe the table completely from existance if it exists?? */
IF OBJECT_ID('temp..#tblACRWeekHours_temp') IS NOT NULL
DROP TABLE #tblACRWeekHours_temp
CREATE	TABLE	#tblACRWeekHours_temp 
	ssn char(9),
	division char(2), 
	pay_sect char(3), 
	currentPayrollDate datetime,
	week int,
	hours_already_worked float,
	_1_0_hrs float,
	_1_5_hrs float,
	_2_0_hrs float,
	_diff_hrs float,
	_eper_hrs float,
	addnl_money float,
	work_location int,
	std_hours float
execute [CalculateHoursAlreadyWorkedThisCycleDuplicate] '...', '...', '...', 37.5

Open in new window

Watch Question
Database Consultant
Top Expert 2009
This problem has been solved!
Unlock 3 Answers and 13 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE