Execute a SQL agent job based off a constraint - sql-server

How do you execute a SQL server agent job in a schedule only if a certain criteria is met.
My example is that I only want to run the job if a boolean value on a different server is set to true otherwise it must not run

Schedules are not conditional. They run at the time they are scheduled, no matter what.
If you want your job to do something only when certain conditions are met, you have to expect that the job will always run on schedule, and put something in the steps that the job executes to check the condition.
So if your job executes SQL Statements, you would put IF blocks in your SQL. If it executes an SSIS package, you would start the package by checking a condition, and so on.

Related

SSIS Job run in loop

I have an SSIS job which pulls data from one database and pushes into another. Currently the actions are triggered when a record is inserted into a table.
My understanding is using a SQL Server trigger to launch an SSIS Job is not advised. Suggesting to me the preferred route for this use case is to use a recurring schedule.
If I schedule every 10 seconds, will the ETL job launch again if the previous run has not finished? (Is there a better word to describe this behavior in the computing spacing?) If the job relaunches, is there a preferred way to accomplish this behavior?
If I schedule every 10 seconds, will the ETL job launch again if the previous run has not finished?
No. The next run time is computed once the job finishes, based on the "Starting at" and the next interval that meets the cycle interval.
While it is running the "Start Job at Step" option on the SQL Server Management Studio interface will be grayed out.
If you try to kick off the job again forcefully using sp_start_job, you'll get a error message saying it's already running.

AD-HOC SQL Server Agent Job

Dynamic creation and deletion of an SQL Agent Job
I have created a stored procedure that creates a number of dynamic agent jobs. The stored procedure creates/runs then deletes an Agent Job perfectly. This does mean that there could be a number of jobs running at once.
This allows me to spawn a number of parallel running jobs, shifting data around rather than doing it sequentially.
At each stage success or failure is logged to a table so at a glance I can see what has failed or completed.
Is the use of spawning multiple Agent jobs from a stored procedure like this practical?
It works well and cuts my load time considerably.

Start a job in SQL SERVER after another job succeeds

I would like to run a procedure in MSSQL based on result of some step in other job.
What I want to achieve:
I want to run a specific procedure after LOAD of data. I dont want to set job start time only at specific time but I want to condition run also on success of other step in other job. But I want tu run a job only if some step in other job was successful. And I want to run it at specific time.
The catch is that this procedure is in other JOB. These two procedures are not in the same JOB.
Is it possible to make a condition like this?
Thank you.

How do you run SQL Server Merge Replication Jobs sequentially?

I work with an environment that uses Merge Replication to publish a dozen publications to 6 a dozen subscribers every 10 minutes. When certain jobs are running simultaneously, deadlocks and blocking is encountered and the replication process is not efficient.
I want to create a SQL Server Agent Job that runs a group of Merge Replication Jobs in a particular order waiting for one to finish before the next starts.
I created an SSIS package that started the jobs in sequence, but it uses sp_start_job and when run it immediately starts all the jobs so they are running together again.
A side purpose is to be able to disable replication to a particular server instead of individually disabling a dozen jobs or temporarily disabling replication completely to avoid 70+ individual disablings.
Right now, if I disable a Merge Replication job, the SSIS package will still start and run it anyway.
I have now tried creating an SSIS package for each Replication Job and then creating a SQL Server Agent job that calls these packages in sequence. That job takes 8 seconds to finish while the individual packages it is calling (starting a replication job) takes at least a minute to finish. In other words, that doesn't work either.
The SQL Server Agent knows when a Replication job finishes! Why doesn't an SSIS package or job step know? What is the point of having a control flow if it doesn't work?
Inserting waits is useless. the individual jobs can take anywhere from 1 second to an hour depending on what needs replicating.
May be I didn't see real problem but it is naturally that you need synchronization point and there are many ways to create it.
For example you could still run jobs simultaneously but let first job lock a resource that is needed for second, that will wait till resource will be unlocked. Or second job can listen log table in loop (with wait for a "minute" and self cancel after "an hour")...

Execute long running jobs in SQL Server

I am working with SQL Server 2008. Using the Agent, I have created a job and scheduled it to execute every minute.
The job executes a stored procedure that moves data from table XXX, to a temp table, and then eventually into table YYY.
The execution of the job may take more than one minute - since the data is rather large.
Will a second instance of the job be started even though the first instance is still running?
If so, should I mark records in temp table (status = 1) to indicate that those records are being processed by a previous instance of the job?
Is there a way for me to check that an instance of the job is currently running, so that I don't initiate a second instance of the job?
Is there another solution for this that I am unaware of? (throughput is important)
Only one instance of a particular job can run at any one time.
So there is no need to take any particular precautions against another execution of the same job beginning before the first one has stopped.
check this post
How to Prevent Sql Server Jobs to Run simultaneously
How to Prevent Sql Server Jobs to Run simultaneously
As Well HERE
Running Jobs
http://technet.microsoft.com/en-us/library/aa213815(v=sql.80).aspx
If a job has started according to its schedule, you cannot start another instance of that job on the same server until the scheduled job has completed. In multiserver environments, every target server can run one instance of the same job simultaneously.

Resources