Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Stored Proc - Dealing with Orders and OrderDetails...


Stored Proc - Dealing with Orders and OrderDetails...

Author
Message
stephenmbell
stephenmbell
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 101
In my database, I have 2 tables, Orders and OrderDetails. This may be obvious, but Orders holds information relevant (DateOrdered, OrderStatus, CustomerNumber) and the OrderDetails table contains information relevant to the details (ProductOrdered, ProductPrice, Shipped)

I want to know the best way to set it up so that when all of the Details of an Order are market Shipped, the order then gets an OrderStatus as Shipped. In addition, for example, if at some point in the process a all of the details get marked as shipped, the OrderStatus changes to Shipped - and someone goes and unmarks a detail as shipped for some reason - the order is then marked back to Pending.

I would think that this is a pretty common scenario - I am just looking for some direction.

Thanks in advance.
sb
Lowell
Lowell
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14942 Visits: 38929
steve whenever i have an overall status that depends on the details, i do not store it in the parent/Orders table. I always try to calculate it based on the details, for example.

I create a view instead, and the view calculates the status, so it is automatically correct based on the details.
an example might be something like this: notice the case statement and the group by sub select to figure it all out:

CREATE TABLE Orders (
OrderID int identity(1,1) not null primary key,
DateOrdered,
CustomerNumber)

CREATE TABLE OrderDetails(
DetailsID int identity(1,1) not null primary key,
OrderID int references Orders(OrderID)
ProductOrdered varchar(30)
ProductPrice decimal(19,4)
Shipped char(1) default('N') )

CREATE VIEW VW_ORDERS AS
SELECT
Orders.OrderID,
Orders.DateOrdered,
Orders.CustomerNumber
CASE
WHEN DETAILS.SHIPPEDCOUNT = DETAILS.TOTALCOUNT
THEN 'Shipped'
WHEN DETAILS.SHIPPEDCOUNT =0
THEN 'Not Shipped'
WHEN DETAILS.SHIPPEDCOUNT < DETAILS.TOTALCOUNT
THEN 'Partial Shipment'
END As Status
FROM VW_ORDERS
LEFT OUTER JOIN
(SELECT OrderID,
SUM(CASE WHEN Shipped = 'Y' THEN 1 ELSE 0 END) AS SHIPPEDCOUNT,
COUNT(DetailsID) AS TOTALCOUNT
FROM OrderDetails
GROUP BY OrderID) DETAILS
ON OrderDetails.OrderId = DETAILS.OrderID



Lowell

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search