Friday, March 23, 2012

question about looping through a record set

I am wondering if someone can help me write a stored procedure to solve
this problem without using a cursor, which I gather should be avoided
if at all possible from what I've read here.
My stored proc needs to return a list of statuses from a table for a
given id number. 98% of the time, this will return one record, but the
other 2% it will be more than one record. I need to loop through these
statuses and make sure they are one of two possibilities, and then if
they are great, make sure they are part of my record set that I return.
If not, do not include them.
Here is at least the first part,
CREATE PROCEDURE prcCheckOrderComplete (@.OrderNum int, @.shipped int,
@.cancelled int) AS
select iItemStatusCodeID from tblShoppingCart where iOrderID =
@.OrderNum
Now, the pseudocode --
/*pseudocode*/
foreach iItemStatusCodeID
if ((iItemStatusCode == @.shipped) || (iItemStatusCode ==
@.cancelled))
great! add it to an array to be returned.
Thanks for any assistance.You are too focused on row-by-row processing. Why not just say
WHERE iOrderID = @.OrderNum
AND (iItemStatusCode IN (@.shipped, @.cancelled));
<danielle.m.manning@.gmail.com> wrote in message
news:1140795611.590886.314190@.e56g2000cwe.googlegroups.com...
>I am wondering if someone can help me write a stored procedure to solve
> this problem without using a cursor, which I gather should be avoided
> if at all possible from what I've read here.
> My stored proc needs to return a list of statuses from a table for a
> given id number. 98% of the time, this will return one record, but the
> other 2% it will be more than one record. I need to loop through these
> statuses and make sure they are one of two possibilities, and then if
> they are great, make sure they are part of my record set that I return.
> If not, do not include them.
> Here is at least the first part,
> CREATE PROCEDURE prcCheckOrderComplete (@.OrderNum int, @.shipped int,
> @.cancelled int) AS
> select iItemStatusCodeID from tblShoppingCart where iOrderID =
> @.OrderNum
> Now, the pseudocode --
> /*pseudocode*/
> foreach iItemStatusCodeID
> if ((iItemStatusCode == @.shipped) || (iItemStatusCode ==
> @.cancelled))
> great! add it to an array to be returned.
>
> Thanks for any assistance.
>|||You are right! I am. Thanks for the tip, i think that will work. I'm
working with perl 99% of the time, which is probably why I am thinking
that way.

No comments:

Post a Comment