Looking for T-SQL scripts to delete a SQL Job
Asked Answered
P

3

6

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

Pinkham answered 2/8, 2009 at 16:16 Comment(0)
A
17
USE msdb;

GO

EXEC sp_delete_job
    @job_name = N'NightlyBackups' ;

GO
Avens answered 2/8, 2009 at 16:28 Comment(0)
C
3

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

Coan answered 2/8, 2009 at 16:20 Comment(1)
How about just execute USE msdb ; GO EXEC sp_delete_job @job_name = $(Job name here); GOPinkham
M
2

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.

Milliemillieme answered 3/8, 2009 at 2:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.