[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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
0
mainrotor
Asked:
mainrotor
1 Solution
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now