Click here to Skip to main content
15,886,074 members
Articles / Database Development / SQL Server
Tip/Trick

Generate Calender Recurring Dates Automatically with SQL Job

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
16 May 2013CPOL2 min read 20.4K   458   5   3
I had a requirement to create automatic date entries into Scheduler SQL table. Whenever a Schedule with RecurrenceRange was set with 'No EndDate', the dates will automatically get added from backend for next year through a job.

Image 1

Introduction

This tip will help to automatically create recurring entries in schedule table.

Background

From frontend, the schedule with 'No EndDate' was created manually for one year (refer to the screenshot above).
The sample screenshot is for the scheduler set as:

  • Recurrence Pattern occurring Daily only on weekdays.
  • Recurrence Range with Start date 13th Feb 2014 and having 'No EndDate'.
    To avoid infinite entries in PatrolScheduleDetail child table, the actual entries here from Front end were created only for one year. Hence, I need to create a mechanism which will automatically create future one year entries.

SQL job will automatically calculate new entries as per the 'Recurrence Pattern': Hourly, Daily, Weekly, Monthly, Yearly. For this purpose, I have written a job and sps in SQL which will create new entries for next year, for the schedule which is about to expire with 'No EndDate'.
To get a quick idea about this tip, I urge you to go through the 'Useful links' which I have given at the bottom of this article.

Using the Code

To use these scripts, a job needs to be setup in SQL which will run the main sp 'usp_GenerateNextYearSchedulePlannerMain' and this will call internally another sps according to Recurrence pattern set for the schedule type.

//
// Example :- As explained above, in backend following process will occur. 
//Sample seed data with scripts is available in the attached files.
// 
// Here Patrol_Schedule parent table will have single entry as provided 
// 'Patrol_Schedule_Insert_Daily_EveryWeekDay.sql' script
// insert into [Patrol_Schedule] 
//  values
// (46, 1, '2013-03-27 00:00:00.000', '2013-03-27 00:30:00.000', 
// 1, 1, 0, 0, 2, 1, 127, 1, 1, 1, 0, '2013-03-27 00:00:00.000', '2014-03-26 00:00:00.000', 1)
//  
//  Patrol_Schedule_Detail child table will have total 261 entries for this schedule for 1 year.
//  For eg. Starting from 13th Feb 2014 till 12th Feb 2015.
//  
//  As my problem statement was to create automatic entries of creating similar 
//  entries for the next year from 2015. I have created these scripts which will 
//  run automatically through job.
//  
//  Attached scripts are :- 
//  Sample seed data Insert scripts
//  - Patrol_Schedule_Table
//  - Patrol_Schedule_Detail_Table
//  - Patrol_Schedule_Insert_Daily_EveryWeekDay
//  
//  Job 
//  - SeedScript_CreateJobCreateNextYearSchedule
//  
//  Stored Procedures required for job
//  - usp_GenerateNextYearSchedulePlannerMain
//    -- usp_GenerateNextYearSchedule_HourlyPattern_Insert
//    -- usp_GenerateNextYearSchedule_DailyPattern_Insert
//    -- usp_GenerateNextYearSchedule_WeeklyPattern_Insert
//    -- usp_GenerateNextYearSchedule_MonthlyPattern_Insert
//    -- usp_GenerateNextYearSchedule_YearlyPattern_Insert
//    --- usp_Patrol_Schedule_Detail_InsertTable
//   
//  These sps will create automatic entries for one year.

Points of Interest

This is my first tip. Though I am not a very good writer, I hope that you find this useful. If you find this tip correct/incorrect, express this fact by rating the tip as you see fit. In the end, you're very welcome at any moment to freely contribute to this project by suggesting improvements, or by submitting code or other materials.

Thanks.

Useful Links

It was my days efforts to write these sps with the help of the above links I mentioned. So I hope it will help someone to save their time with my tip. Thanks to my wife Geeta who encouraged me to write this stuff and to CodeProject for posting my first tip.

History

  • 15th May, 2013: Original post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
MCP, MCAD.NET, MCSD.NET in .NET technology. MCTS in .NET Framework 3.5, ASP.NET Applications, MS (Microsoft Specialist).
PGDIT in IT (Symbiosis).

Doing programming into .Net Technology from year 2004.
Worked extensively on both windows and web platform.
In my projects I have worked on Asp.Net(1.1, 2.0, 3.5, 4.7), C#, VB.Net, MVC, Angularjs 1.6, Angular 2+, Typescript, SignalR, Linq, Dependency Injection, Design Pattern, Bootstrap and Sql Server(2000,2005, 2008, 2012).
HTML5, JScript, CSS, XML, AJAX, Webapi, WCF & Web Services, WPF, MVC.

Since you have gone through my profile and would like to share any job opportunities for me, please contact me at pavan.thakur@gmail.com

I love programming and like to share my knowledge.
Cheers Smile | :)

Comments and Discussions

 
GeneralRequired Info Pin
Patel Amit R8-May-15 0:37
Patel Amit R8-May-15 0:37 
QuestionFunction requirements Pin
Member 1128773810-Dec-14 18:58
Member 1128773810-Dec-14 18:58 
QuestionRequired Info Pin
Member 106543738-Mar-14 11:53
Member 106543738-Mar-14 11:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.