SQL Server 2005: Schedule Start/Stop of TSQL Script - sql-server

Currently running SQL 2005 on Server 2005. I have a TSQL script which updates 1000 rows at a time. It loops through a counter until there are no rows to update.
In my situation, I have a lot of rows to update and I need to run this "after hours". So I would like to see if there is a way that I can schedule a way to automatically start and stop a task based upon a set time. I am thinking that I could place this task in a SP and start it with SQL Agent. However, I can not think of how to stop the task automatically. I'm open to SSIS too.

You can use a SQL Job, and make this call a stored procedure or some sql. The job can be set on a timer, for specific days etc.
See Creating a Job for help

You can schedule following T-SQL to stop jab as well.
USE msdb ;
GO
EXEC dbo.sp_stop_job N'Your job name' ;
http://technet.microsoft.com/en-us/library/ms182793.aspx

Related

Schedule SQL Job X minutes after SQL Server Agent starts

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.

Schedule a stored procedure to run periodically?

I need to run a task to update a table, periodically, using a stored procedure. Could you let me know if it is possible to schedule periodic tasks and to run any system command within stored procedure?
You can use SQL Server Agent to create jobs that run at regularly-scheduled intervals. One of the job step types is executing a stored procedure.
I use a 3rd party tool for my schedules. To be honest, it's not a great deal better than Sql Server Agent, but there are few perks to it. It's called
System Scheduler.

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...

How to customize sql server agent jobs

I need to be able to schedule a job on the 10th of each month and have it run for a set number of days or until a specified date.
Is there any way to customize sql server agent jobs using some sort of API or something?
Thanks.
You can accomplish using the SQL Job Schedule properties by using more than one schedule for the job. You can schedule the job to run on the 10th day of the month (with the option for an end date). You can then create multiple schedules, one for the 11th, 12th, etc.
The SQL Server Agent does not have a schedule option like you need, so I think your workaround is as good as it gets for keeping it inside SQL Server.
The Windows Task Scheduler however does have an option to run a job on the 'x' day of the month, maybe kicking your sproc off using sqlcmd via Windows Task Scheduler is an alternate solution?
Hope this helps
I still can't find any way of customizing the job schedule, but I have figured a way around it by adding the condition to the job step:
IF (DAY(GetDate()) >= 10)
EXEC MySP
And setting the schedule to run every day.

Resources