更改sql server上的触发器以调用代理作业

时间:2012-05-30 08:37:23

标签: sql-server-2008 triggers sql-agent-job

我已经完成了一个触发器,如果​​条件符合条件(如果表有超过999个项目),则会调用名为“RefileFromQue”的代理作业。 我的aproach不起作用: 1.我不希望每次将一个项插入表时都会运行此触发器,因为此表“忙”并且我不希望触发器占用大量内存空间。 2.我知道在我的情况下使用“AFTER INSERT”并不好,所以我想改变它。 3.我也尝试过运行EXEC msdb.dbo.sp_start_job @Job_Name ='RefileFromQue'

这是我的代码:

USE [ACTIVE]
GO
/****** Object:  Trigger [dbo].[MoreThan1000InRefileQueue]    Script Date: 05/30/2012 11:09:44 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Hadas
-- Create date: 30/05/2012
-- Description: Handles an "overflow" in Worksoace_RefileQueue (more than 1000 items)
-- =============================================
ALTER TRIGGER [dbo].[MoreThan1000InRefileQueue] ON  [dbo].[Workspace_RefileQueue] AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for trigger here
    DECLARE @Count int
    SELECT @Count = (SELECT Count(*) FROM [Workspace_RefileQueue])
    IF @Count > 999 
    BEGIN
        EXEC msdb.dbo.sp_start_job 'RefileFromQue' /*@Job_Name = 'RefileFromQue'*/  
    END
END

0 个答案:

没有答案