SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 stored proc help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

agastyamahi
Starting Member

Canada
32 Posts

Posted - 11/06/2013 :  15:13:08  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

3706 Posts

Posted - 11/06/2013 :  15:28:50  Show Profile  Reply with Quote
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

Canada
32 Posts

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

agastyamahi
Starting Member

Canada
32 Posts

Posted - 11/07/2013 :  01:14:19  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 11/07/2013 :  03:03:18  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000