Any solutions to test SQL Agent Job immediately to ignore schedule - sql

I am using SQL Server 2008 and I want to test the execution correctness of my SQL Server Agent job immediately to ignore the schedule. Any ideas?
thanks in advance,
George

In SSMS, under the "SQL Server Agent" node, open the "Jobs" subnode and find your job, right click on it and select "Start Job" - that'll start and run the job right away.
Marc

Create a stored procedure that encapsulates all the aspects of your job and then use that in SQL Agent. You can then just call the procedure from the command line to test it eg. exec dbo.MyProcedure #param1 = 'foo'

Change the schedule to have it run five minutes in the future.
Then get off the server so it runs in exactly the environment you want to test.

Related

Executing muliple Stored Procedures within a single SQL Server Agent Job

My SQL Server Agent job seems to execute the 1st SP cmd but not the second. Does anybody know how i get multiple commands into a single Server Agent job?
Use the GO command after executing each stored procedure.
Do you reference any FilteredViews in your sproc code? Then check this post and see if you can implement any of these solutions.

How to run a stored procedure automatically every day

How do I set up to run a stored procedure automatically every day in SQL Server 2008 R2?
Set up a SQL job http://msdn.microsoft.com/en-us/library/ms135739.aspx
You need to use the Job scheduler in the sql agent. Sql express doesn't include it so I just have a batch file run as a scheduled task to run it.
-James
Was discussed here:
Scheduled run of stored procedure on SQL server
Under SQL agent you need to go to the job scheduler and create a job that runs the stored proc. Once you have created the job you can create one or more schedules for the job. http://msdn.microsoft.com/en-us/library/ms135739.aspx

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

How to run a stored procedure every day in SQL Server Express Edition?

How is it possible to run a stored procedure at a particular time every day in SQL Server Express Edition?
Notes:
This is needed to truncate an audit table
An alternative would be to modify the insert query but this is probably less efficient
SQL Server Express Edition does not have the SQL Server Agent
Related Questions:
How can I schedule a daily backup with SQl Server Express?
Scheduled run of stored procedure on SQL Server
Since SQL Server express does not come with SQL Agent, you can use the Windows scheduler to run a SQLCMD with a stored proc or a SQL script.
http://msdn.microsoft.com/en-us/library/ms162773.aspx
I found the following mechanism worked for me.
USE Master
GO
IF EXISTS( SELECT *
FROM sys.objects
WHERE object_id = OBJECT_ID(N'[dbo].[MyBackgroundTask]')
AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[MyBackgroundTask]
GO
CREATE PROCEDURE MyBackgroundTask
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- The interval between cleanup attempts
declare #timeToRun nvarchar(50)
set #timeToRun = '03:33:33'
while 1 = 1
begin
waitfor time #timeToRun
begin
execute [MyDatabaseName].[dbo].[MyDatabaseStoredProcedure];
end
end
END
GO
-- Run the procedure when the master database starts.
sp_procoption #ProcName = 'MyBackgroundTask',
#OptionName = 'startup',
#OptionValue = 'on'
GO
Some notes:
It is worth writing an audit entry somewhere so that you can see that the query actually ran.
The server needs rebooting once to ensure that the script runs the first time.
Create a scheduled task that calls "C:\YourDirNameHere\TaskScript.vbs" on startup. VBScript should perform repeated task execution (in this example, it's a 15 minute loop)
Via command line (must run cmd.exe as administrator):
schtasks.exe /create /tn "TaskNameHere" /tr "\"C:\YourDirNameHere\TaskScript.vbs\" " /sc ONSTARTUP
Example TaskScript.vbs: This executes your custom SQL script silently using RunSQLScript.bat
Do While 1
WScript.Sleep(60000*15)
Set WshShell = CreateObject("WScript.Shell")
WshShell.RUN "cmd /c C:\YourDirNameHere\RunSQLScript.bat C:\YourDirNameHere\Some_TSQL_Script.sql", 0
Loop
RunSQLScript.bat: This uses sqlcmd to call the database instance and execute the SQL script
#echo off
sqlcmd -S .\SQLEXPRESS -i %1
If you are using Express Edition, you will need to use the Windows Scheduler or the application connecting to the server in some way.
You would use the scheduler to run sqlcmd. Here are some instructions for getting the sqlcmd working with express edition.
SQL Scheduler from http://www.lazycoding.com/products.aspx
Free and simple
Supports all versions of SQL Server 2000, 2005, and 2008
Supports unlimited SQL Server instances with an unlimited number of jobs.
Allows to easily schedule SQL Server maintenance tasks: backups, index rebuilds, integrity checks, etc.
Runs as Windows Service
Email notifications on job success and failure
Since another similar question was asked, and will likely be closed as a duplicate of this one, and there are many options not mentioned in the answers already present here...
Since you are using SQL Express you can't use SQL Server Agent. However there are many alternatives, all of which you can schedule using AT or Windows Task Scheduler depending on your operating system:
VBScript
C# command line app
batch file with SQLCMD
PowerShell
All of these languages/tools (and many others) have the capacity to connect to SQL Server and execute a stored procedure. You can also try these Agent replacements:
SQLScheduler
Express Agent
Standalone SQL Agent (beta)
The easiest way I have found to tackle this issue is to create a query that executes the stored procedure then save it. The query should look similar to this one below.
use [database name]
exec storedproc.sql
Then create a batch file with something similar to the code below in it.
sqlcmd -S servername\SQLExpress -i c:\expressmaint.sql
Then have the task scheduler execute the batch as often as you like
Another approach to scheduling in SQL Express is to use Service Broker Conversation Timers. To run a stored procedure periodically, which you can use to bootstrap a custom scheduler.
See eg Scheduling Jobs in SQL Server Express
You could use Task Scheduler to fire a simple console app that would execute the Sql statement.
As you have correctly noted, without the agent process, you will need something else external to the server, perhaps a service you write and install or Windows scheduler.
Note that with an Express installation for a local application, it is possible that the machine may not be on at the time you want to truncate the table (say you set it to truncate every night at midnight, but the user never has his machine on).
So your scheduled task is never run and your audit log gets out of control (this is a problem with SQL Server Agent as well, but one would assume that a real server would be running non-stop). A better strategy if this situation fits yours might be to have the application do it on demand when it detects that it has been more than X days since truncation or whatever your operation is.
Another thing to look at is if you are talking about a Web Application, there might be time when the application is loaded, and the operation could be done when that event fires.
As mentioned in the comment, there is sp_procoption - this could allow your SP to run each time the engine is started - the drawbacks with this method are that for long-running instances, there might be a long time between calls, and it still has issues if the engine is not running at the times you need the operation to be done.
Our company also use SQLEXPRESS and there is no SQL Agent.
Since there is no marked answer as "right" and all the solutions are quite complex I'll share what I did there. May be its really bad, but it worked great to me.
I've chosen operations of Insertion (people do) to a table that got closely the same time range i needed and made a trigger "ON INSERT" that applies needed function.

Looking for T-SQL scripts to delete a SQL Job

If I know the database server name, instance name and the SQL Server job name, how to delete a SQL Server job by its name in a simple way? I am writing scripts which will be called by sqlcmd to delete SQL jobs.
Appreciate if anyone could show me a sample? :-)
thanks in advance,
George
USE msdb;
GO
EXEC sp_delete_job
#job_name = N'NightlyBackups' ;
GO
You're looking for sp_delete_job:
[srv].[master].[dbo].sp_delete_job #job_name = 'MyJob'
So this four part name only works with linked servers. Otherwise, you'll have to connect to the server, and run that command against it (with everything right of [dbo]..
It's worth noting that you can just use SSMS, choose the job, right-click and pick "Delete", and then use the Script button at the top of the dialog box to generate a script like the ones suggested here.