| Author |
Topic |
|
crugerenator
Posting Yak Master
126 Posts |
Posted - 2008-12-03 : 15:30:23
|
I've got a problem where I have 3 select statements that all need to be in one stored procedure. The issue is that the first select statement returns a list of storeID's. Each of these storeID's is the where clause in my second and third select statements. What i'm doing right now is populating a recordset w/ my first select statement, then performing the second and third select statement as I loop through each value of the recordset. This works, but I now need the entire process to be in a stored procedure. Here's my code:select orderIDfrom [Order]where [Order].orderDate between @date1 and @date2 and [order].storeID = @storeID--this select statement currently populates an asp record set that is then looped through, --performing the below 2 select statements on each value in the recordset--ORDER BREAKOUTselect description, dvalue, quantity, sum ((ItemType.dvalue*OrderItem.quantity)) as total from [Order] join OrderItem on OrderItem.orderID = [Order].orderID join ItemType on orderItem.ItemTypeID = OrderItemType.ItemTypeIDwhere [Order].orderID = @orderID --orderID comes from first select statement group by description, dollarValue, quantity--TOTALselect sum ((ItemType.dvalue*OrderItem.quantity)) as total from [Order] join OrderItem on OrderItem.orderID = [Order].orderID join OrderItemType on orderItem.ItemTypeID = OrderItemType.ItemTypeIDwhere [Order].orderID = @orderID --orderID comes from first select statement So basically, how do I populate a record set, loop through that recordset and perform 2 selects on each value in that recordset in a single stored procedure?Thanks, any help is appreciated |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
crugerenator
Posting Yak Master
126 Posts |
Posted - 2008-12-03 : 16:08:46
|
quote: Originally posted by tkizer Rather than loop, which is a performance issue, can you bring back two result sets that contain the rows for all of the ones in the first select statement? This would be a set-based approach and therefore more efficient.
Yeah, I guess I could. Any idea on how I can go about doing this? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-12-03 : 16:12:53
|
Try this for the first one:select [Order].orderID, description, dvalue, quantity, sum ((ItemType.dvalue*OrderItem.quantity)) as total from [Order]join OrderItem on OrderItem.orderID = [Order].orderIDjoin ItemType on orderItem.ItemTypeID = OrderItemType.ItemTypeIDwhere [Order].orderDate between @date1 and @date2 and [order].storeID = @storeIDgroup by [Order].orderID, description, dollarValue, quantity Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jholovacs
Posting Yak Master
163 Posts |
Posted - 2008-12-03 : 16:23:23
|
I would write it as follows:--ORDER BREAKOUTSELECT o.OrderID, description, dvalue, quantity, SUM((it.dvalue*oi.quantity)) AS total FROM [Order] oINNER JOIN OrderItem oi ON oi.orderID = o.orderIDINNER JOIN ItemType it ON oi.ItemTypeID = it.ItemTypeIDWHERE o.orderDate BETWEEN @date1 AND @date2 AND o.storeID = @storeIDGROUP BY o.OrderID, description, dvalue, quantity--TOTALSELECT o.orderID, SUM((it.dvalue*oi.quantity)) AS totalFROM [Order] oINNER JOIN OrderItem oi ON oi.OrderID = o.OrderIDINNER JOIN ItemType it ON oi.itemTypeID = it.ItemTypeIDWHERE o.orderDate BETWEEN @date1 AND @date2 AND o.storeID = @storeIDGROUP BY o.orderID Now granted, this is not exactly what you asked, but this should be much cleaner. You will no longer need to return a result set of OrderID's and iterate through them, and you can get both results back with one set of parameters from a single stored procedure, which as far as I can tell, are your primary requirements. SELECT TOP 1 w.[name]FROM dbo.women wINNER JOIN dbo.inlaws i ON i.inlaw_id = w.parent_idWHERE i.net_worth > 10000000 AND i.status IN ('dead', 'dying') AND w.husband_id IS NULLORDER BY w.hotness_factor DESC |
 |
|
|
jholovacs
Posting Yak Master
163 Posts |
Posted - 2008-12-03 : 16:24:17
|
eh... tara beat me to it... SELECT TOP 1 w.[name]FROM dbo.women wINNER JOIN dbo.inlaws i ON i.inlaw_id = w.parent_idWHERE i.net_worth > 10000000 AND i.status IN ('dead', 'dying') AND w.husband_id IS NULLORDER BY w.hotness_factor DESC |
 |
|
|
crugerenator
Posting Yak Master
126 Posts |
Posted - 2008-12-03 : 17:16:42
|
| Thanks guys, this is perfect. |
 |
|
|
crugerenator
Posting Yak Master
126 Posts |
Posted - 2008-12-03 : 17:48:55
|
| Actually have a stupid Q... Is there anyway to add breaks when OrderID's change?Example:orderId111111<br /><br />2222222<br /><br /> |
 |
|
|
jholovacs
Posting Yak Master
163 Posts |
Posted - 2008-12-03 : 17:56:52
|
I think that would have to be managed in your code. I'm sure that you could do that in SQL, but it really be much faster and cleaner on your web server. SELECT TOP 1 w.[name]FROM dbo.women wINNER JOIN dbo.inlaws i ON i.inlaw_id = w.parent_idWHERE i.net_worth > 10000000 AND i.status IN ('dead', 'dying') AND w.husband_id IS NULLORDER BY w.hotness_factor DESC |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-12-03 : 19:03:48
|
quote: Originally posted by crugerenator Actually have a stupid Q... Is there anyway to add breaks when OrderID's change?Example:orderId111111<br /><br />2222222<br /><br />
That is definitely an application code/presentation layer issue and should not be addressed in the database. Typically you should only return raw data from the database. The application should handle all formatting.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
|