Trigger a SQL Server job from another job - sql-server-2012

There is job which first generates the data in initial steps and in later steps it then copies the data to 4 different servers. Since I wanted the parallel execution I created another job which handles copying the data to 2 servers and originally existing job copies data to 2 servers. I want some mechanism to trigger the new job after the initial steps of generation of data of exiting job runs successfully. Any help would be appreciated!

You can add the below command wherever you want to start the other job execution:
EXEC msdb.dbo.sp_start_job 'other_job_name'

exec msdb.dbo.sp_start_job #job_name = 'job_name';

Related

SQL Agent - Can I delete an intermediate job step without consequences?

Say I have a job in SQL Server with 8 steps.
If I run a script to delete an intermediate job step, like such:
EXEC msdb.dbo.sp_delete_jobstep #job_id=N'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', #step_id=5
What happens? Will the job run as scheduled, and just skip from step 4 to step 6? Will the job break?
Every Step in Steps tab have two property :
On success action
On failure action
check these, there is no problem with deleting steps if you handle that.

SQL Server 2008 Agent jobs. Two procedures and a view

I'm using two stored procedures a view to create report.
Every 3 months, I have to update these two stored procedures and view automatically.
First, I should update stored procedure 1
Then, I need to update view (it takes data from stored procedure 1)
Finally, I need to update stored procedure 2 (it takes data form stored procedure 1 and from the view)
To do this, I would like to use SQL Server Agent jobs. Is it possible to run one SQL Server Agent job and updated these two store procedure and view in that sequence? or should I create SQL Server Agent job with command to update stored procedure 1 then I need to create another job with command to update view and then create another job with command to stored procedure 2.
Thank you for any suggestions.
Yes, the tasks that are described in the question can be done with one single SQL Server Agent Job.
For this SQL Server Agent Job you may want to set several steps
- Step 1: run SP1
- Step 2: update view
- Step 3: run SP2)
OR actually create only one step that will have all the actions inside. Also, the job should have the 3 months schedule created as needed.
Note: you refer as "update stored procedure 1" .. I think you actually mean "execute stored procedure 1".
For further documentation I recommend: http://msdn.microsoft.com/en-us/library/ms190268(v=sql.105).aspx

Parallel execution of stored procedures in job (SQL Server)

Breif:
I have five stored procedures and each of it have no dependencies. The identical stuff is that it pulling data from five different servers. We are just collating and feeding it to our server.
Question:
We have scheduled all these five in single job with 5 different steps. I want to execute it in parallel instead of sequential order.
Extra:
I can actually collate all five stored procs in to one if its possible to run it in parallel in sp level (if not possible in job level).
Thanks
The job steps are always executed in succession (hence 'steps').
To parallelize it, create five jobs with the same schedule.
There are several options. 2 of which are :
Create a SSIS job to run the 5 stored procedures in parallel.
Create 5 different SQL Server Agent jobs scheduled at the same time
It's also possible to create a main job and add steps to call child jobs (each calling it's own stored procedure) asynchronously as suggested at https://dba.stackexchange.com/questions/31104/calling-a-sql-server-job-within-another-job/31105#31105
However for more complicated flow it's better to use SSIS packages which are design to handle different workflows.

SQL how to make a job run a job?

I'd like to call another job immediately after first one has finished, or to be more precise is it possible to call an entire sql job via a job step. I'd like to avoid merging these jobs into 1 so I wonder if this solution is possible?
Yes, you can execute a job by using this stored procedure. In your case, you can simply add a step to the end of your first job, to call the name of the job you want executed next.
EXEC msdb.dbo.sp_start_job N'Job Name';
See sp_start_job (Transact-SQL) for more information.
create a T-SQl Step and use EXEC msdb.dbo.sp_start_job N'YourJob';
Call the Jobs in the order you wish from a stored procedure.

SQL Server Agent Jobs: How to execute a job step without executing the entire job

I have a SQL Server Agent Job that previously had two steps. Today, I've had to integrate the third step, and soon I'll need to integrate a fourth.
I want to be sure that the step will execute properly but I do not want to execute the entire job.
The first two steps take quite a bit of time to execute and during the day they hog a significant amount of the SQL resources that my users need.
Is there a way that I can execute a job step and not an entire job process?
Within SSMS, if I right-click on the job there is an option that states "Start Job at step..." except when I try that option it brings up a dialog that seems to imply that the entire job has been started. What can I do to test one step within a job?
Thanks in advance.
"Start job at step" will start the job at the step you specify. However - if you don't wish to execute any subsequent steps - be sure to adjust the step logic so that it will "Quit reporting success" after completing the step you started at.
In SSMS:
Copy the code from the job step (use ctrl + a to select all)
Paste the code in to a new query window in SSMS
Run the code
Alternately if you have a recurring need to run just a couple steps.
Put those steps in a separate job, then kick off one job from the other to run everything. Use EXEC msdb.dbo.sp_start_job N'job_name'
You can run the short easy job on demand, and the full long job as scheduled
Use this:
EXEC msdb.dbo.sp_start_job N'job_name' , 'step_name'
Go to job properties and manually do that step. For example, there is a job with 16 steps and you want to run the 12th step, then go to that job property and see what that step 12 exactly does. If it executes a batch file in command prompt, do that manually. But this logic cannot be used in all cases.
USE [**DB_NAME**]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[**STEP_NAME**]
SELECT 'Return Value' = #return_value
GO