English 中文(简体)
Cannot Delete a SQL job
原标题:
  • 时间:2009-11-25 10:12:45
  •  标签:
  • sql
  • jobs

I have disabled log shipping on a SQL 2005 database and deleted the log shipping DB on the secondary server. However i cannot delete the LSRestore_DB___ job, either by T-SQL (sp_delete_log_shipping_primary_secondary, sp_delete_job) or using the management studio on the secondary server. It just wont go. The query keeps on executing for a good 7 hours. Tried disabling, still doesn t delete. Restarted the server too. Also tried the Can anyone help me delete this SQL job please ?

问题回答

There is a good article Can’t Delete Jobs.In the article the author provided a script to solve the problem,good job!

`CREATE PROC dbo.DropJob
@JobName AS VARCHAR(200) = NULL 
AS 
DECLARE @msg AS VARCHAR(500);

IF @JobName IS NULL
BEGIN 
SET @msg = N A job name must be supplied for parameter @JobName. ;
RAISERROR(@msg,16,1);
RETURN;
END
IF EXISTS (
SELECT subplan_id FROM msdb.dbo.sysmaintplan_log WHERE subplan_id IN
( SELECT subplan_id FROM msdb.dbo.sysmaintplan_subplans WHERE job_id IN 
(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = @JobName)))
BEGIN
DELETE FROM msdb.dbo.sysmaintplan_log WHERE subplan_id IN
( SELECT subplan_id FROM msdb.dbo.sysmaintplan_subplans WHERE job_id IN 
(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = @JobName));

DELETE FROM msdb.dbo.sysmaintplan_subplans WHERE job_id IN
(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = @JobName);

EXEC msdb.dbo.sp_delete_job @job_name=@JobName, @delete_unused_schedule=1;
END
ELSE IF EXISTS (
SELECT subplan_id FROM msdb.dbo.sysmaintplan_subplans WHERE job_id IN 
(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = @JobName))
BEGIN 
DELETE FROM msdb.dbo.sysmaintplan_subplans WHERE job_id IN 
(SELECT job_id FROM msdb.dbo.sysjobs_view WHERE name = @JobName);

EXEC msdb.dbo.sp_delete_job @job_name=@JobName, @delete_unused_schedule=1;
END
ELSE
BEGIN 
EXEC msdb.dbo.sp_delete_job @job_name=@JobName, @delete_unused_schedule=1; 
END 
GO`

Now you can call the SP with the following;

`USE [msdb];
EXEC dbo.DropJob @JobName = N Shrink_AWP_Databases.Subplan_1 ;`

Have your tried setting the allow editing of system tables, and going directly into the system table that holds the job information and tried deleting the row from there?

Make sure to be extra careful when doing this, not recommended, but sometimes direct editing to the system tables needs to be done.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签