We help IT Professionals succeed at work.
Get Started

Why can't I run a select command in an IF ELSE SQL 2000 statement

woodwyn
woodwyn asked
on
431 Views
Last Modified: 2012-05-12
Can someone tell me why this won't work and what an alternative approach might be?  I am trying to run one of two select statements in a stored procedures based on whether a parameter = 0 or 1.  I simplified the code here for testing in analyzer.  Whether the parameter is set to 0 or 1, when I run the statement I get the error "There is already an object named '#MSR1' in the database."  It appears to hit both select commands each time.

DECLARE @nCreatedForJob AS TinyInt
SET @nCreatedForJob=1

IF @nCreatedForJob = 1
      BEGIN
      DROP TABLE #MSR1
      SELECT * INTO #MSR1 FROM Items WHERE Number='L0100001' AND Quantity=1
      END
ELSE
      BEGIN
      DROP TABLE #MSR1
      SELECT * INTO #MSR1 FROM Items WHERE Number='L0100001'
      END
Comment
Watch Question
Senior Developer
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 20 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