I need help using IF ELSE in a SQL Server 2008 stored proc

Hi Experts,
I need help using an IF statement in my stored procedure.
I currently do a SELECT STATEMENT QUERY that I use top place values into variables.  I want to do an if statement based on thge value of my @BillOption variable.

Here is my select statement

SELECT TOP(1) @Amount = O.ShipmentAmount,
@BillOPtion = O.BillOption
FROM Orders O
WHERE OrderNumber = @OrderNumber


I want my if statement to do the following:

IF my @BillOption = 'GROUND' I want my insert statment to look like this:

INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
SELECT CUSTNUMBER, SHIPAMOUNT, ORDERSTATUS)
FROM INTERORDERS
WHERE OrderNumber = @OrderNumber


ELSE
IF my @BillOption = 'COLLECT' I want my insert statment to look like this:


INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
SELECT CUSTNUMBER, @Amount, @BillOPtion)
FROM EXTERORDERS
WHERE OrderNumber = @OrderNumber



How can I do this?  Thank you very much in advance.

mrotor
mainrotorAsked:
Who is Participating?
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.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
That would be a couple of CASE blocks

INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
SELECT CUSTNUMBER, 
  CASE WHEN @BillOption = 'GROUND' THEN SHIPAMOUNT WHEN @BillOption = 'COLLECT' THEN @Amount END, 
  CASE WHEN @BillOption = 'GROUND' THEN ORDERSTATUS WHEN @BillOption = 'COLLECT' THEN @BillOPtion END, 
FROM EXTERORDERS
WHERE OrderNumber = @OrderNumber

Open in new window

Here's an article on SQL Server CASE Solutions that is a larger demo of CASE.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
The question is what do you want to do if @billion is neither of the 2  values...

As the FROM is different tables , you can use indeed if statment, or you could use a UNION ALL structure
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Disregard my first comment, just noticed that the FROM table is different.

Give this a whirl..

IF @BillOption = 'GROUND'
   INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
   SELECT CUSTNUMBER, SHIPAMOUNT, ORDERSTATUS)
   FROM INTERORDERS
   WHERE OrderNumber = @OrderNumber
ELSE IF @BillOption = 'COLLECT'
   INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
   SELECT CUSTNUMBER, @Amount, @BillOPtion)
   FROM EXTERORDERS
   WHERE OrderNumber = @OrderNumber
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dukster131Commented:
You can just use two if statements - I like to bracket the information in a Begin End statement

IF my @BillOption = 'GROUND'
Begin
 INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
 SELECT CUSTNUMBER, SHIPAMOUNT, ORDERSTATUS)
 FROM INTERORDERS
 WHERE OrderNumber = @OrderNumber
End

  IF my @BillOption = 'COLLECT'
 Begin

INSERT INTO OrderShipment(CustNumber, ShipmentAmount, OrderStatus)
 SELECT CUSTNUMBER, @Amount, @BillOPtion)
 FROM EXTERORDERS
 WHERE OrderNumber = @OrderNumber

End
0
mainrotorAuthor Commented:
Duskter131,
What are the benefits of bracketing the information with Begin and End statements?

mrotor
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
It's to make code more readable.
For a single statement it's not required.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.