views:

496

answers:

3

Hello everyone,

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

+2  A: 

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

Eric
How about just execute USE msdb ;GOEXEC sp_delete_job @job_name = $(Job name here);GO
George2
+5  A: 

USE msdb;

GO

EXEC sp_delete_job @job_name = N'NightlyBackups' ;

GO

Sergey Olontsev
Your solution works!
George2
+2  A: 

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.

Rob

Rob Farley