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
 stored proc help

Author  Topic 

agastyamahi
Starting Member

33 Posts

Posted - 2013-11-06 : 15:13:08
Hello,

I have the command below:

Execute sp_RevenueSummary_SEL @ai_Year = 2013, @ai_Month = 10, @ai_CurrencyID = 1, @ac_UserName = ptjohn, @ac_IsController = N
Execute sp_RevenueSummary_SEL @ai_Year = 2013, @ai_Month = 10, @ai_CurrencyID = 1, @ac_UserName = ptjohn, @ac_IsController = Y

I need to get the open month and year and run these commands for that month + 12 months going forward.

Once I get this, I can create a automated job that runs every hour. Could you please let me know how I can get the month incremented automatically, should I write an if condition for each month and year. Please let me know

Thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-11-06 : 15:28:50
Something like this?
DECLARE @year INT;
DECLARE @month INT;

SET @year = 2012; SET @month = 1;

-- run your stored procedure with @year and @month.

--- REPEATING SECTION
SET @month = CASE WHEN @month = 12 THEN 1 ELSE @month+1 END;
SET @year = CASE WHEN @month = 1 THEN @year+1 ELSE @year END;
-- run your stored procedure AGAIN with @year and @month.
--- END OF REPEATING SECTION
You could put the code into a loop if you like.

Another alternative would be to modify the stored procedure to accept a start date and end date and run for all date ranges within the stored proc.
Go to Top of Page

agastyamahi
Starting Member

33 Posts

Posted - 2013-11-06 : 22:21:09
I am sorry, its still not clear. Can you please elaborate ?
Go to Top of Page

agastyamahi
Starting Member

33 Posts

Posted - 2013-11-07 : 01:14:19
I just want from the current month to 12 months forward, does this work ?

CREATE Procedure [dbo].[sp_RevenueCalculation_Routine] AS

DECLARE @Year integer;
DECLARE @Month Integer;
SET @Year = 2013; SET @Month = 11;

while(@month<=12 AND @year <=2014)
BEGIN
if(@month = 12)
BEGIN
Exec sp_RevenueSummary_SEL @ai_Year = @Year, @ai_Month = @Month, @ai_CurrencyID = 1, @ac_UserName = 'xxxx', @ac_IsController = 'N';
Exec sp_RevenueSummary_SEL @ai_Year = @Year, @ai_Month = @Month, @ai_CurrencyID = 1, @ac_UserName = 'xxxx', @ac_IsController = 'Y';
set @month =1;
set @Year = @year+1;
END
ELSE
Exec sp_RevenueSummary_SEL @ai_Year = @Year, @ai_Month = @Month, @ai_CurrencyID = 1, @ac_UserName = 'xxxx', @ac_IsController = 'N';
Exec sp_RevenueSummary_SEL @ai_Year = @Year, @ai_Month = @Month, @ai_CurrencyID = 1, @ac_UserName = 'xxxx', @ac_IsController = 'Y';


END
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-07 : 03:03:18
I would strongly suggest to change the storedprocedure to accept a daterange instead of start year/month. That way you dont have to call it inside a loop which will degrade the performance. You can generate the start and end dates using below formula


DECLARE @Year int,@Month int
SELECT @Year=YEAR(GETDATE()),@Month=MONTH(GETDATE())

SELECT @StartDate = DATEADD(mm,@Month-1,DATEADD(yy,@Year-1900,0)) --this will give you current month start,
@EndDate = DATEADD(mm,13,@StartDate)-1 -- This will giave you last day for current month + 12

Now pass these dates as start and end dates after changing the procedure


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

- Advertisement -