触发器无法处理Update sql

时间:2016-12-01 21:25:29

标签: sql sql-server triggers

我编写了一个简单的存储过程,它应该根据另一个不断添加值的表b来更新现有的表。

我希望每次向b添加值时都运行存储过程。

我写了一个简单的触发器,我觉得应该可行,但它不起作用。每次在b中插入或更新时,触发器就会执行存储过程。

这是触发器的样子:

CREATE TRIGGER [dbo].[TriggerTest]
ON [dbo].[BELL_DPRA2_locates_fact]
AFTER UPDATE, INSERT
AS
BEGIN
    EXECUTE dbo.Test
END

如果我单独运行程序,它可以正常工作。它更新了第一个表。 但如果我让它等待触发器动作,则没有任何反应。同时,表b每秒都有数据流。请帮我找出问题所在。

这是存储过程:

CREATE PROCEDURE [dbo].[Test]
AS 
BEGIN
    DECLARE @CurDate DATE
    SET @CurDate = CAST((SELECT GETDATE()) AS DATE); 

    MERGE INTO dbo.DPRA2_Export T
    USING (SELECT
               CAST(risk_date AS DATE) as "Dated", 
               COUNT(risk_score) AS "Risk2" 
           FROM 
               dbo.BELL_DPRA2_locates_fact  
           WHERE 
               Risk_Score = 2 
               AND CAST(Risk_Date AS DATE) = @CurDate
           GROUP BY 
               CAST(risk_date AS DATE)) S ON T.Risk_Date_Day = S.Dated

    WHEN MATCHED THEN 
        UPDATE SET T.Risk2 = S.Risk2

    WHEN NOT MATCHED THEN
        INSERT (Risk_Date_Day, Risk2) 
        VALUES (@CurDate, S.Risk2);
END

0 个答案:

没有答案