sql trigger - 按时间限制

时间:2015-10-09 14:25:37

标签: sql-server triggers

我希望此触发器仅在特定时间和另一时间(例如上午6点至晚上10点)之间起作用。请帮忙!

ALTER TRIGGER [db].[el] ON [Reports].[db].[stat] 
AFTER INSERT, UPDATE
AS

SET NOCOUNT ON;

INSERT INTO [Reports].[db].[el]
            (
               [StationID]
              ,[Count]

              )
 SELECT i.StationID,
        i.[EmptyDockCount],
        GETDATE(),
        NULL,
        NULL,
        i.[LastUpdateDate],
 FROM INSERTED i
 INNER JOIN DELETED d
 on d.StationID = i.StationID
 INNER JOIN DBOS.dbo.StationDim bsd
 ON bsd.StationID = i.StationID
 WHERE i.[Count] = 0
 AND d.[count] <> 0

 ;

1 个答案:

答案 0 :(得分:2)

试试这个,

这是关键声明CONVERT(TIME,Getdate())BETWEEN'6:00:00.0000000'''22:00:00.0000000'

 ALTER TRIGGER [db].[el]
    ON [Reports].[db].[stat]
    AFTER INSERT, UPDATE
    AS
        SET NOCOUNT ON;

        IF CONVERT(TIME, Getdate()) BETWEEN '6:00:00.0000000' AND '22:00:00.0000000'
          BEGIN
              INSERT INTO [Reports].[db].[el]
                          ([StationID],
                           [Count])
              SELECT i.StationID,
                     i.[EmptyDockCount],
                     Getdate(),
                     NULL,
                     NULL,
                     i.[LastUpdateDate]
              FROM   INSERTED i
                     INNER JOIN DELETED d
                             ON d.StationID = i.StationID
                     INNER JOIN DBOS.dbo.StationDim bsd
                             ON bsd.StationID = i.StationID
              WHERE  i.[Count] = 0
                     AND d.[count] <> 0
          END;