I have many Transact-SQL (SQL 2005) scripts in a folder meant to run as a whole unique Job.
I would like to run them one after the other as “scheduled sub-jobs” (one click run) in a ordered way based on the file name (file system folder with many *.sql files) e.g.:
01_init_vars.sql
05_create_this_thing.sql
10_create_another_thing.sql
15_calculate_this_value_based_on_today_date.sql
20_create_that_thing.sql
25_summarize_finish.sql
Every SQL script acts on a different part of the Database, sometimes they use the same tables, but they act at different “logic levels” in different time stamps (i.e. I need to run first one script, then another script, etc.).
All these scripts share “environment variables” that I would like to set only one time in the first script and use them as “SQL session variables” e.g.:
DECLARE @Username NVARCHAR(100)
SET @Username = 'Blahblahblah'
I was wondering if there is a Microsoft SQL Server Management Studio Tool to merge and run them all, or (better for me) an example of Stored Procedure to run.
Please note that inside some of these “sub-jobs” there can be a Stored Procedure, then I would like to prehvent issues due to Stored Procedures defined and run inside other Stored Procedures, if there are any.
I am so sorry for this very straightforward question, but after querying Google a lot of times with different key words, after looking at standard transact-SQl statements (MSDN website) I am not able to find a proper answer and I think this should be an “usual way” of running tasks on a SQL Database, probably I am missing something and I can’t find what.
Thanks!
If you’re sharing variables between the scripts, you probably want to investigate Sql Sever Integration Services (SSIS).
You can control the flow of data between scripts, set up conditional ‘routing’ for the order in which things run and much more that you probably won’t need for this task.
If you can’t use SSIS for whatever reason, why not just create a new Job in Sql Server Agent and add each script as a new Step in the job? If you need to save results from one for use in another, perhaps just create a table to hold these values for the duration of the job and have the final script remove them “on the way out” as it were.