Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Loop Date Range Expand / Collapse
Author
Message
Posted Tuesday, December 23, 2008 8:28 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, April 15, 2009 1:56 PM
Points: 13, Visits: 55
I am trying to write a store procedure that will extract data from the 1st of the month up-to current date within that month - ending on the last day of the month. For instances today is the 23rd - I need to extract data from a table where the date starting from the 1st through the 23rd, however I must extract ONE DAY at a time, therefore the query must loop through the dates.

I have a very complexed query already in place that calls on several functions - I can only process 1 day at a time.

How do I reloop through a query extracting data from the 1st up-to the current date one day at a time?
Post #624725
Posted Tuesday, December 23, 2008 8:45 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 2:20 PM
Points: 3,122, Visits: 11,401
Did you have a question that you wanted to ask?
Post #624749
Posted Tuesday, December 23, 2008 10:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 9:07 AM
Points: 11,157, Visits: 12,899
Why must you return only 1 row at a time? Couldn't you return the data as a set and let the business layer or UI handle the looping? IMO that would be more effecient, as you would have one call to the DB and the rest done in memory. If you post some DDL, test data, and expected/desired results as mentioned in the link in my signature then you will likely get some solutions proposed.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #624847
Posted Tuesday, December 23, 2008 10:59 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 3:48 PM
Points: 4,388, Visits: 9,506
Why do you have to loop? What is the requirement that states you have to process a single day at a time?

FWIW, you could build a stored procedure that accepts as input the date you want to search, and query for everything on that one date. Example:

CREATE PROCEDURE dbo.MySingleDaySearch
@inputDate datetime
AS

DECLARE @startDate datetime;
DECLARE @endDate datetime;

SET @startDate = dateadd(day, datediff(day, 0, @inputDate), 0); -- remove time portion
SET @endDate = dateadd(day, 1, @startDate); -- set the end date to tomorrow

SELECT ...
FROM dbo.MyTable t
WHERE t.MyDateColumn >= @startDate
AND t.MyDateColumn < @endDate;
GO

But, I gotta say that running this for a full month by 'looping' on each date is not the right way to solve the problem. If you posted the problem you are trying to solve - it might be easier for us to recommend a better solution than looping.


Jeffrey Williams
Problems are opportunites brilliantly disguised as insurmountable obstacles.

How to post questions to get better answers faster
Managing Transaction Logs
Post #624881
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse