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
 Not using loops - to loop

Author  Topic 

richardlaw
Yak Posting Veteran

68 Posts

Posted - 2013-05-04 : 15:14:32
Hi

I have a table of classes. Each class runs only once a week, and the classes table stores the day it runs as an INT (1 = Mon, 2 = Tue, … 7 = Sun).

I now need to create a temp table that list all the classes that take place within a date range (like a calendar of events). So for example, if the first class runs even Monday, and my date range is over 4 weeks and 3 days, I would expect to see in my temp table 5 rows (date, classID) – assuming one of the 3 days is a Monday.

I assume I would need a loop of some kind that filters each date in the date range to check if a class runs on that day, if so, add a row to the temp table with the date and classID; if not, move on to the next class before moving on to the next day (date + 1 day).

I’d really like to find a way that does not require loops. The method above seems very process intensive. I’m new to TSQL, but looping like that just feels clumsy.

Any ideas?
Thanks as always

MuMu88
Aged Yak Warrior

549 Posts

Posted - 2013-05-04 : 23:06:53
Refer to
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185092 &
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=185095
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-06 : 02:03:12
The best thing in your case might be to use a calendar table as base and then join to your table based on the dates. Then you can filter on day field to give you only mondays

see

http://visakhm.blogspot.in/2010/02/generating-calendar-table.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -