How do I execute SQL Agent job 3 times in a row?
We are creating Data Warehouse Project. It ETLs data from Flatfile Legacy Folder, and creates Dim and Fact tables.
SSIS Project Environment Variable (FlatfileFolder) set: to March 2018
- Import ETL Flatfiles into Staging Database for March 2018
- Create Customer Dimension Tables
- Create Product Dimension Tables
- Create SalesTransaction Fact Tables
Want to execute this 3 times in a row, changing Project Environment Variable (FlatfileFolder) to April 2018.
Run 4 jobs again,
then change Project Environment Variable (FlatfileFolder) to May 2018, and run 4 job steps again. (Theoretically, should have been designed to extract all history files and load at once, however did not design this, just inheriting it.)
This stack link below talks about endless loop, only want to run it 3 times, and change environment variable.
Options:
(a) write 12 Step SQL Agent Job,
(b) Or somehow after Step 4, on success go to back to Step 1? How to make this only run 3 times?
Eventually may want to execute job 5-10 times in row for later months.
Trying to conduct with SQL environment, prefer to refrain from C# or Powershell unless required.
We are processing data.
Thanks,
Best Answer
I think I would solve it this way:
WHILE LOOP
to check IF work is to be done. Use the custom stored procedure sp_sp_start_job_wait to invoke the job that has the 4 steps. This procedure will start the job and wait for it to complete.WHILE LOOP
re-iterates to check for more work to be done.To start the entire process, simply invoke the driving stored procedure.
You could also eliminate the need for the SQL Agent jobs altogether and simply invoke the SSIS packages directly from the driving stored procedure I described earlier using built-in stored procedures; e.g.
ssisdb.catalog.create_execution
,ssisdb.catalog.start_execution
. There is a good blog post describing that process How To Execute an Integration Services (SSIS) Package from a SQL Server Stored Procedure. Summarizing the main points from that post:I highlighted the fact that the default behavior of
ssisdb.catalog.start_execution
is to run asynchronous, but there is a workaround for that as documented in this post by Tim Mitchell. See the section Execute Packages Synchronously. Quoting from that post:Force a Script Failure if the Package Fails