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
 need help with a stored proc

Author  Topic 

agastyamahi
Starting Member

33 Posts

Posted - 2013-11-26 : 15:49:06
Hi, I have a query below

BEGIN

DECLARE @CalDate datetime
SET @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 int
set @CalMonth = datepart(mm, @CalDate)

DECLARE @CalYear int
SET @CalYear = datepart(yyyy,@CalDate)

DECLARE @count INT
SET @count = 1

WHILE(@Count <=12)
BEGIN

Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth
Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth
set @CalMonth = @CalMonth + 1

SET @Count = @Count+1
END
END

My 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 have

Thanks




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 parameters

Be One with the Optimizer
TG
Go to Top of Page

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 below

BEGIN

DECLARE @CalDate datetime
SET @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 int
set @CalMonth = datepart(mm, @CalDate)

DECLARE @CalYear int
SET @CalYear = datepart(yyyy,@CalDate)

DECLARE @count INT
SET @count = 1

WHILE(@Count <=12)
BEGIN

Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth
Exec sp_CalendarProc @ai_Year = @CalYear, @ai_Month = @CalMonth
set @CalMonth = @CalMonth + 1

SET @Count = @Count+1

IF (@Count > 12)
BEGIN
@CalMonth = 1
@CalYear = @CalYear + 1
END

END
END

My 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 have

Thanks






Go to Top of Page

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

Go to Top of Page

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 basics
this is a good site to get basics

http://www.w3schools.com/sql/

follow forums like these. try sugestions yourself and learn concepts. Next step try to answer questions asked
Then your coding skills will certainly improve.

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

- Advertisement -