Home All Groups Group Topic Archive Search About

looping in stored procedure

Author
31 Aug 2006 5:10 PM
laurie
Hi,

I have two tables (county(countyID, countyName) and results(resultsID,
countyID, resultDate, fileName, rptYearQuarter).  Each county can have zero
or more records in the results table.  I need to be able to get the most
recent fileName for each county for a specific rptYearQuarter.

I have a stored procedure that selects the top 1 (most recent) fileName for
a specific county and rptYearQuarter. 

I also have a stored procedure that loops through and calls the previous
stored procedure for each county.

It does return the results for each county, but in 58 different result sets.
How can I combine those sets into one table?

Thanks!!

--
Laurie Duresky
..net Developer

Author
31 Aug 2006 5:18 PM
Tracy McKibben
laurie wrote:
Show quote
> Hi,
>
> I have two tables (county(countyID, countyName) and results(resultsID,
> countyID, resultDate, fileName, rptYearQuarter).  Each county can have zero
> or more records in the results table.  I need to be able to get the most
> recent fileName for each county for a specific rptYearQuarter.
>
> I have a stored procedure that selects the top 1 (most recent) fileName for
> a specific county and rptYearQuarter. 
>
> I also have a stored procedure that loops through and calls the previous
> stored procedure for each county.
>
> It does return the results for each county, but in 58 different result sets.
>  How can I combine those sets into one table?
>
> Thanks!!
>

If you want a band-aid fix, you could create a temp table in the "outer"
sproc, insert the results of each execution of the "inner" sproc into
that temp table.  After you're done looping, simply select everything
from that temp table, returning it as a single resultset.

The proper fix would be to rewrite the whole mess so that you're not
looping at all.  SQL works best with set-based operations, looping is
probably the worst way to implement anything in SQL.  You have the query
that determines the most recent filename, and you have the query that
determines the list of counties.  Work the two together into a single
query.  Much more elegant, and will perform drastically better than the
looping method.  This link will show you how to join on the "latest"
value:
http://realsqlguy.com/serendipity/archives/10-Gimme-The-Latest-And-Greatest!.html


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
31 Aug 2006 5:32 PM
laurie
Thanks, that's a good start, but in that example, what if you needed the
information for every book together, not just a specific one?  Would you just
add PriceList.BookID = 1 or PriceList.BookID = 2 or PriceList.BookID = 3 etc?
I need the ID to be from 1 to 58.

Thanks again!
--
Laurie Duresky
..net Developer


Show quote
"Tracy McKibben" wrote:

> laurie wrote:
> > Hi,
> >
> > I have two tables (county(countyID, countyName) and results(resultsID,
> > countyID, resultDate, fileName, rptYearQuarter).  Each county can have zero
> > or more records in the results table.  I need to be able to get the most
> > recent fileName for each county for a specific rptYearQuarter.
> >
> > I have a stored procedure that selects the top 1 (most recent) fileName for
> > a specific county and rptYearQuarter. 
> >
> > I also have a stored procedure that loops through and calls the previous
> > stored procedure for each county.
> >
> > It does return the results for each county, but in 58 different result sets.
> >  How can I combine those sets into one table?
> >
> > Thanks!!
> >
>
> If you want a band-aid fix, you could create a temp table in the "outer"
> sproc, insert the results of each execution of the "inner" sproc into
> that temp table.  After you're done looping, simply select everything
> from that temp table, returning it as a single resultset.
>
> The proper fix would be to rewrite the whole mess so that you're not
> looping at all.  SQL works best with set-based operations, looping is
> probably the worst way to implement anything in SQL.  You have the query
> that determines the most recent filename, and you have the query that
> determines the list of counties.  Work the two together into a single
> query.  Much more elegant, and will perform drastically better than the
> looping method.  This link will show you how to join on the "latest"
> value:
> http://realsqlguy.com/serendipity/archives/10-Gimme-The-Latest-And-Greatest!.html
>
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
>
Author
31 Aug 2006 5:43 PM
Tracy McKibben
laurie wrote:
> Thanks, that's a good start, but in that example, what if you needed the
> information for every book together, not just a specific one?  Would you just
> add PriceList.BookID = 1 or PriceList.BookID = 2 or PriceList.BookID = 3 etc?
>  I need the ID to be from 1 to 58.
>
> Thanks again!

If you needed every book, you would simply remove

WHERE PriceList.BookID = 1

Or, if as you say, you need ID's from 1 to 58, but you have ID's from 1
to 100 in your table

WHERE PriceList.BookID BETWEEN 1 AND 58


--
Tracy McKibben
MCDBA
http://www.realsqlguy.com
Author
31 Aug 2006 5:30 PM
David Portas
laurie wrote:
Show quote
> Hi,
>
> I have two tables (county(countyID, countyName) and results(resultsID,
> countyID, resultDate, fileName, rptYearQuarter).  Each county can have zero
> or more records in the results table.  I need to be able to get the most
> recent fileName for each county for a specific rptYearQuarter.
>
> I have a stored procedure that selects the top 1 (most recent) fileName for
> a specific county and rptYearQuarter.
>
> I also have a stored procedure that loops through and calls the previous
> stored procedure for each county.
>
> It does return the results for each county, but in 58 different result sets.
>  How can I combine those sets into one table?
>
> Thanks!!
>
> --
> Laurie Duresky
> .net Developer

Like this. No need for a loop (think like SQL, not like .NET :-)

SELECT c.countyid, c.countyname,
   (SELECT TOP 1 r.filename
    FROM results AS r
    WHERE r.countyid = c.countyid
     AND rptyearquarter = @rptyearquarter
    ORDER BY r.resultdate DESC, r.resultsid DESC) AS filename
FROM county AS c ;

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--

AddThis Social Bookmark Button