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.
Author |
Topic |
agastyamahi
Starting Member
33 Posts |
Posted - 2013-11-26 : 15:49:06
|
Hi, I have a query belowBEGIN DECLARE @CalDate datetimeSET @CalDate = (select (DateAdd(mm, 1, Max (Cast((Cast(CalYear AS CHAR(4)) + '/' + Cast(CalMonth AS CHAR(2)) + '/' + '01') AS DATETIME))))FROM CalRecord WHERE CalType = 'F' And CalStatus = 1)declare @CalMonth intset @CalMonth = datepart(mm, @CalDate)DECLARE @CalYear intSET @CalYear = datepart(yyyy,@CalDate) DECLARE @count INTSET @count = 1WHILE(@Count <=12)BEGIN Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonthExec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonthset @CalMonth = @CalMonth + 1SET @Count = @Count+1ENDENDMy CalRecord table has the Year and Month column, with data for 2013(Month 1 to 12) and 2014(Month 1 to 12)in it. My @CalDate returns 2013-06-01, now I need to run stored proc with year = 2013 and month = 6. After this, in the similar way I want my month to be incremented(and year to be incremented after 2013/12), so I added the set @CalMonth = @CalMonth + 1 statement, but I am running into issues since I am getting data for month 13,14,15....but I should not. I need the stored proc to run only from 2013-06 to 2014-05.Could you please correct the query I haveThanks |
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2013-11-26 : 16:35:44
|
probably best to modify sp_CalendarProc so that it takes a date range and operates as a set-based operation with one call.But this will resolve you immediate problem:WHILE(@Count <=12)BEGIN set @calMonth = datepart(month, @calDate) set @calYear = datepart(year, @calDate) select @calMonth, @calYear Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth SET @Count = @Count+1 set @calDate = dateadd(month, 1, @calDate)END EDIT:spelling correction on @cal parametersBe One with the OptimizerTG |
 |
|
bitsmed
Aged Yak Warrior
545 Posts |
Posted - 2013-11-26 : 16:38:02
|
See red comments below - mind you, this has not been testet, so syntax errors might occur.quote: Originally posted by agastyamahi Hi, I have a query belowBEGIN DECLARE @CalDate datetimeSET @CalDate = (select (DateAdd(mm, 1, Max (Cast((Cast(CalYear AS CHAR(4)) + '/' + Cast(CalMonth AS CHAR(2)) + '/' + '01') AS DATETIME))))FROM CalRecord WHERE CalType = 'F' And CalStatus = 1)declare @CalMonth intset @CalMonth = datepart(mm, @CalDate)DECLARE @CalYear intSET @CalYear = datepart(yyyy,@CalDate) DECLARE @count INTSET @count = 1WHILE(@Count <=12)BEGIN Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonthExec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonthset @CalMonth = @CalMonth + 1SET @Count = @Count+1IF (@Count > 12)BEGIN @CalMonth = 1 @CalYear = @CalYear + 1ENDENDENDMy CalRecord table has the Year and Month column, with data for 2013(Month 1 to 12) and 2014(Month 1 to 12)in it. My @CalDate returns 2013-06-01, now I need to run stored proc with year = 2013 and month = 6. After this, in the similar way I want my month to be incremented(and year to be incremented after 2013/12), so I added the set @CalMonth = @CalMonth + 1 statement, but I am running into issues since I am getting data for month 13,14,15....but I should not. I need the stored proc to run only from 2013-06 to 2014-05.Could you please correct the query I haveThanks
|
 |
|
agastyamahi
Starting Member
33 Posts |
Posted - 2013-11-26 : 16:55:28
|
Thank you very much that worked.I need a suggestion, I am starting my career in SQL, I am unable to code based on the requirement sometimes. Could you please let me know how I can improve my coding skills |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-27 : 04:34:38
|
quote: Originally posted by agastyamahi Thank you very much that worked.I need a suggestion, I am starting my career in SQL, I am unable to code based on the requirement sometimes. Could you please let me know how I can improve my coding skills
start with learning basicsthis is a good site to get basicshttp://www.w3schools.com/sql/follow forums like these. try sugestions yourself and learn concepts. Next step try to answer questions askedThen your coding skills will certainly improve.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|