首页 > 解决方案 > SQL Server : delay trigger

问题描述

I have customer machine what inserts data into my SQL Server database at midnight. In SQL Server there is trigger to delete old records upon insert.

The problem is, the customer does bunch of single insert commands (instead of using bulk insert), actually hundreds of them. I cannot control that. What I did is to record last trigger time and if datediff is less than one hour, don't do anything. I read using WAITFOR in triggers is a bad idea as it locks the table until trigger execution is done. Is there any other way? Cheers.

BEGIN
    SET NOCOUNT ON;

    IF DATEDIFF(HOUR, (SELECT TOP 1 TimeStamp 
                       FROM HouseKeepingStats 
                       ORDER BY TimeStamp DESC), GETDATE()) > 1 
    BEGIN
        EXEC HouseKeeping;
    END
END

标签: sql-servertriggersdatabase-trigger

解决方案


推荐阅读