SQL how to make a job run a job? - sql

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.

Related

Trigger a SQL Server job from another job

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';

How to execute multiple stored procedures at a time?

I want to know that is there any way through which I can execute all my stored procedure at a time.
Presently I am executing each stored procedure using exec "Stored Procedure name" command.
And I have more than 200 stored procedure to be executed on my database.
Is there any easy way out to execute all these stored procedure at a single time as it is difficult for me to keep a track of them?
I suggest you write a stored procedure which calls the other ones.
Put all stored procedures inside a stored procedure,
CREATE PROCEDURE CallAllProcedure
AS
BEGIN
CALL Proc1
CALL Proc2
END
Assuming that you are using Query Analyzer, just put a GO in between all those stored proc and run script.
If you want to execute them all in parallel you could create a SQLJob and schedule them all to execute at the same time. Link below is general usage of SQL Jobs.
http://msdn.microsoft.com/en-us/library/ms190268.aspx
you can select all stored procedure names from sys.objects table querying type='P'. After you can use cursor for every stored procedure name to execute. But how about stored procedures with parameters? you must provide parameter values as well to avoid from errors.
You can use Service broker to do this async but I dont think it is a great idea to run 200 stored procs at the same time unless you are sure there will not be any contention on the DB

SQL Server 2008 : Re-runnable stored procedure

I want to make my stored procedure re-runnable.
If it started and run partial way and failed because of a reason, it should be re-runnable.
Please suggest some ways to do that.
Thanks !
You can create a JOB and trigger it in the stored procedure.
Specify the retry times and the retry interval on the Step (work) that you want to re-run.
here's a reference on how to start a Job.
http://www.mssqltips.com/sqlservertip/1730/different-ways-to-execute-a-sql-agent-job/
If you just need to execute your stored procedure from beginning after failure, you can catch the failrue (using TRY .. CATCH or ##ERROR) and then redirect the execution to the begin of the stored procedure using GOTO

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

Launch multiple stored procedures to run 'in the background' on SQL Server Express Edition

Is it possible to run multiple stored procedures that run 'in the background'?
The stored procedures must be launched from a single, master stored procedure, in the same way that multiple worker threads are spawned. For example:
CREATE PROCEDURE MyLauncher
AS
BEGIN
BEGIN
#EXEC MyBackgroundSP01 -- Runs in parallel to the other 2
#EXEC MyBackgroundSP02 -- Runs in parallel to the other 2
#EXEC MyBackgroundSP03 -- Runs in parallel to the other 2
END
END
It is possible in SQL 2005 and later. Have look at http://rusanu.com/2009/08/05/asynchronous-procedure-execution/
No this isn't possible as you have described it. You could run multiple SQL Jobs which will execute the procedures concurrently/
According to this question you could try using the Service Broker
Asynchronous Stored Procedure Calls
If you run they in the same procedure, it will launch in the same thread (and in the same internal transaction, what can make the log very big).
Not with pure T-SQL. But you could write a little dotNET app to run them asynchronously easily enough, as long as you leave the connection option until all three are finished.
IF both Stored procedure have same parameter then you can create a new store procedure
like
create third procedure
(#colname int)
as
begin
exec first procedure
exec second procedure
end
exec third procedure
You can try it. I am sure how appropriate it is.