Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple statements in one stored procedure

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 orderID

from [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 BREAKOUT
select 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.ItemTypeID

where [Order].orderID = @orderID --orderID comes from first select statement

group by description,
dollarValue,
quantity

--TOTAL
select sum ((ItemType.dvalue*OrderItem.quantity)) as total

from [Order]
join OrderItem
on OrderItem.orderID = [Order].orderID
join OrderItemType
on orderItem.ItemTypeID = OrderItemType.ItemTypeID

where [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

Posted - 2008-12-03 : 15:43:33
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.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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?
Go to Top of Page

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].orderID
join ItemType
on orderItem.ItemTypeID = OrderItemType.ItemTypeID
where
[Order].orderDate between @date1 and @date2 and
[order].storeID = @storeID
group by
[Order].orderID,
description,
dollarValue,
quantity


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-12-03 : 16:15:39
Try this for the second one:

select
[Order].orderID,
sum ((ItemType.dvalue*OrderItem.quantity)) as total
from [Order]
join OrderItem
on OrderItem.orderID = [Order].orderID
join OrderItemType
on orderItem.ItemTypeID = OrderItemType.ItemTypeID
where
[Order].orderDate between @date1 and @date2 and
[order].storeID = @storeID
group by [Order].orderID


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

jholovacs
Posting Yak Master

163 Posts

Posted - 2008-12-03 : 16:23:23
I would write it as follows:

--ORDER BREAKOUT
SELECT
o.OrderID,
description,
dvalue,
quantity,
SUM((it.dvalue*oi.quantity)) AS total

FROM
[Order] o
INNER JOIN OrderItem oi
ON oi.orderID = o.orderID
INNER JOIN ItemType it
ON oi.ItemTypeID = it.ItemTypeID
WHERE
o.orderDate BETWEEN @date1 AND @date2
AND
o.storeID = @storeID
GROUP BY
o.OrderID,
description,
dvalue,
quantity

--TOTAL
SELECT
o.orderID,
SUM((it.dvalue*oi.quantity)) AS total
FROM
[Order] o
INNER JOIN
OrderItem oi
ON
oi.OrderID = o.OrderID
INNER JOIN
ItemType it
ON
oi.itemTypeID = it.ItemTypeID
WHERE
o.orderDate BETWEEN @date1 AND @date2
AND
o.storeID = @storeID
GROUP 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 w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page

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 w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page

crugerenator
Posting Yak Master

126 Posts

Posted - 2008-12-03 : 17:16:42
Thanks guys, this is perfect.
Go to Top of Page

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:

orderId
1
1
1
1
1
1
<br />
<br />
2
2
2
2
2
2
2
<br />
<br />
Go to Top of Page

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 w
INNER JOIN
dbo.inlaws i
ON
i.inlaw_id = w.parent_id
WHERE
i.net_worth > 10000000
AND
i.status IN ('dead', 'dying')
AND
w.husband_id IS NULL
ORDER BY
w.hotness_factor DESC
Go to Top of Page

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:

orderId
1
1
1
1
1
1
<br />
<br />
2
2
2
2
2
2
2
<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 Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -