Schedule SQL Job X minutes after SQL Server Agent starts - sql-server

I have a Job in SQL server which is scheduled to 'Start automatically when SQL Server Agent starts', however, this is creating some issues in our environment. The issue is not SQL specific but because of it we need to delay the execution of this particular job. I cannot schedule this as a recurring job and assign a particular time of a day, it needs to run every time when the SQL Server starts. Is there a way in which I can schedule the job that runs after 15 mins after SQL Server Agent starts?

Sure, just make the first step:
WAITFOR DELAY '00:15:00';
(Or, probably better, you could try to resolve whatever "some issues" are.)
However, note that someone can restart the Agent service without restarting SQL Server; or, they could set SQL Server Agent to not automatically start at startup, so that the next time the SQL Server service is restarted, your procedure will not run.
If you want to tie some startup activity to SQL Server starting, you could probably look into startup procedures, but if you need it to wait 15 minutes, the first thing in the procedure would be the above WAITFOR. Also startup procedures can't have input parameters (or output parameters, but that is less likely to be an issue for a stored procedure you're calling from a job).
Finally, SQL Server 2008 R2? That was barely still in support at the beginning of the last decade.

Related

How to start SQL Server Agent etl job after complete Oracle process

I have Oracle source and SQL Server. Every day at 12.00 AM Oracle data populate for oracle database tables, after that sql server agent jobs run manually by me after Oracle Data Extraction completed. for SQL Server databases, I want to automate this process. when oracle data population completed i need to run sql server agent jobs automatically, how can i do this.
You write a little program that will monitor the oracle process and when it "sees" that is has completed (probably successfully - you skipped that part) it starts the sql server job. Now - how do you do that? How do you know that the process both completed and successfully? Go figure it out based on what functionality Oracle offers and (perhaps) the characteristics of the output that signify success.
Perhaps you can vastly simplify things and schedule some sort of "do it" job that will look for "good" output. Schedule it for a time that is more than sufficient for the oracle process to complete. Do some checking and if things look good, start the other job.

SQL Server 2008 R2 Job Launched Step 1 hundreds of times

I have an ETL SSIS package that is scheduled via job to run nightly at 7pm. It is the only step in the job, and the failure action is "quit the job reporting failure". The server is Windows Server 2008 R2, and the SQL Server version is 2008 R2. There is also an instance of SQL Server 2012 installed on this server, but the services are not started for that instance.
I've made no changes to the job, package, or server, and tonight it behaved strangely. When I look at the history of the job and expand tonight, it shows starting step 1 over 400 times, all at exactly 7 PM. It looks like it just kept launching it until the transaction log filled the entire drive and had no more space to grow, then exited the job reporting failure. I shrunk the transaction log by setting recovery mode to simple and running DBCC SHRINKFILE. I then restarted all of the SQL services for that instance and re-ran the job. So far, it seems to be running as expected, although I suppose time will tell.
I did a search of stack overflow and have seen nothing like this mentioned. We're actually starting a project to virtualize the box, then upgrade to 2012, so this may end up being one of those oddball things that never happens again, but I thought I'd ask in case anyone has any idea why this might have happened.
open the job step and go to the advanced tab. Look at the retry attempts. could it be that it has a big number? this would make the step run many times if it fails.
:

schedule sql job in another server via stored procedure

Here is a picture of how things are supposed to work.
I'll log into sql server and manually start jobA. My interaction ends here. This job doesn't have a schedule enabled. Hence it is manually started. It has 3 steps. The 3rd step will execute another job manually (which is on another server). Technically the 3 step should in turn schedule jobA, to run say after 5 minutes (only 1 time).
But how to schedule jobA which is on another server?
Maybe it is better for jobB to wait for 5 minutes and then start jobA (without scheduling).
Assuming it is SQL Server 2005 or newer, You can use WAITFOR:
WAITFOR DELAY '00:05';
Or if You insist on scheduling a job using stored procedure, check sp_add_schedule stored procedure.
If You already knew this and focus of your question is on 'another' server, check: How to run a Job from a Stored Procedure in another server?

SQL Server Agent Job Running Slow

I am executing a stored procedure using SQL Server Agent Job in SQL Server 2005.
This job was running fast until yesterday. Since yesterday this job is taking more than 1 hour instead of 2 mins.
I executed the stored procedure in SSMS, it just took less than 1 minute to execute.
I could not figure out why it is taking more than 1 hour when executed as a SQL Server Agent job?
After some time commenting and assuming that the SP performs with the same input parameters and data well when executed in SSMS, I finnaly think I can give a last tip:
Depending on what actions are performed within the SP (e.g. inserting/updating/deleting a lot of data within a loop or cursor), you should set nocount on at the beginning of your code.
set nocount on
If this is not the case or does not help, please add more information, already mentioned in the comments (e.g. all settings of the Job and each Jobstep, what has been logged, what is in the Jobhistory, check SQLerrorlogs, eventlogs,....).
Also take a look at the "SQL Server Logs" maybe you can gather some info here. Also a look into the Application/System eventlo of the Databaseserver is always a good idea.
To get a basic overview you can use the Activitymonitor in SSMS, by selecting the Databaseserver and selecting "Activity monitor" from contextmenu and search for the sql agent.
My last try would be to try to run a sql trace for the agent. In this case you would start a trace and filter e.g. by the user that the SQLAgent Service runs. There are so many options you can set for traces, so I would recommend to google for it, search on MSDN or ask another question here on stackoverflow.
We have a large proc that runs in 88 seconds in SSMS and 30-45 minutes in SQL Server Agent. I added the dbo. prefix on all the table names and now it runs just as fast as SSMS.
I've noticed that SQL Agent jobs ignore the server's MAXDOP setting and run everything with a MAXDOP of 1. If I run a stored procedure in a query windows, it obeys the server settings and uses 4 processes. If I use SQL Agent, any stored procedure I run uses only one process.
I have a similar issue with a script that calls a number of UDFs that I created. The UDF's themselves normally run subsecond under SSMS. Likewise, running the reports I generate with them is bearable under SSMS (30d data in 8s, 365d data in 22s). I've always done NOCOUNT ON with my SQL Agent jobs as they normally generate text files out for pickin up by other processes or Excel and I do not want the extra data at the end, so it was not a solution for me.
In this case, when we run the exact same script under SQL Agent as a job, my times grow exponentially. My 8s script takes 2m30s and my 22s script takes 2h20m. This is the same whether I run it midday with other user activity and jobs or after hours with no user activity, nor jobs or backups running. Our server is idle and at best I get one of the 8 cores being utilized when run. DB is only about 10GB running on SSD with a cached RAID card and 16 of 32GB RAM is free. Since my SQL runs efficiently in SSMS, I am pretty well of the belief that I am hitting a threading limit of some sort. I have researched and tried adjusting MAXDOP just prior to the scripts in the SQL Agent with no luck.
Since this is an activity I want to schedule, it needs to be automated one way or another. I could let these scripts take the hours they need to run as SQL steps in SQL Agent jobs, but I decided to run from command line instead and I get the same performance I see in SSMS.
sqlcmd -S SQLSRVRHost -i "C:\My Script Loc With Spaces.sql" -v MyVar="VarValue" >"C:\MyOutputFile.txt"
So I created a batch script with the SQL jobs run from sqlcmd. Then I run the batch script from a SQL Agent job, so I still have the same management and control in place. My 4 SQL jobs that collectively took over 3 hours to run complete in 1 min and a few seconds from a single batch script executed by SQL Agent.
I hope this helps...

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