| Author |
Topic |
|
Freddie
Starting Member
29 Posts |
Posted - 2007-11-09 : 16:11:33
|
| Hello everyone:this procedure resets the business day to one for every month. It works fine except for the month of October. any idea or suggestions?DECLARE @i INT SET @i = 1DECLARE @DateID INT, @DtTimeD DATETIME, @LASTDAY DATETIMEDECLARE c CURSORFOR---- LASTDAY is the last day of the month -- the counter will reset to 1 on the first of each month-- SELECT DateID, DtTimeD, DATEADD(dd, -DAY(DATEADD(m,1,DtTimeD)), DATEADD(m,1,DtTimeD)) 'LASTDAY' FROM D_DATE WHERE WkDayIn = 'Yes' AND HolidIn = 'No'OPEN cFETCH NEXT FROM c INTO @DateID, @DtTimeD, @LASTDAYWHILE @@FETCH_STATUS = 0---- update the business day in D_DATE--BEGIN UPDATE D_DATE SET BusDay = @i WHERE DateID = @DateID---- reset the counter to 1 if it's the last day of the month--IF @DtTimeD = @LASTDAY SET @i = 1ELSE SET @i = @i + 1-- IF @@ROWCOUNT = 500 COMMIT--FETCH NEXT FROM c INTO @DateID, @DtTimeD, @LASTDAYENDCLOSE cDEALLOCATE cGO |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 16:14:12
|
Are you using SQL Server 2005? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 16:16:37
|
Because there are 30 days in November and 31 days in October?Because last day of October was Wednesday, neither weekend nor holiday? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 16:19:34
|
You can also use UPDATE .. WHERE CURRENT OF .. to speed you cursor solution up.But I think you do not need a cursor-based solution. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Freddie
Starting Member
29 Posts |
Posted - 2007-11-09 : 16:23:08
|
| i am using SQL Server 2000 and the counter works for other months that have 31 days.... |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 16:27:47
|
Do you have a clustered index on the D_DATE table for DtTimeD column?Or possible the DateID column? And if so, do DateID column correspond to the DtTimeD column? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-09 : 16:36:16
|
Try this oneCREATE TABLE #Stage ( RowID INT IDENTITY(1, 1), DateID INT, theMonth INT, theSequence INT )INSERT #Stage ( DateID, theMonth )SELECT DateID, DATEDIFF(MONTH, '19000101', DtTimeD)FROM D_DATEWHERE WkDayIn = 'Yes' AND HolidIn = 'No'ORDER BY DtTimeDUPDATE sSET s.theSequence = 1 + s.RowID - r.RowID FROM #Stage AS sINNER JOIN ( SELECT theMonth, MIN(RowID) AS RowID FROM #Stage GROUP BY theMonth ) AS r ON r.theMonth = s.theMonthUPDATE wSET w.BusDay = y.theSequenceFROM #Stage AS wINNER JOIN D_DATE AS y ON y.DateID = w.DateIDDROP TABLE #Stage E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Freddie
Starting Member
29 Posts |
Posted - 2007-11-12 : 09:17:29
|
| Thank you very much...works much better than my proc.Have a nice day!!Freddie |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-12 : 09:29:34
|
And much faster too, I can presume? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-11-13 : 02:55:26
|
quote: Originally posted by Peso And much faster too, I can presume? E 12°55'05.25"N 56°04'39.16"
Thats understood by default MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-11-13 : 03:13:00
|
Well, the original error was that it wouldn't work for the month of October.That was the main priority.If I also managed to get the code to run faster.... That's a bonus. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|