MSSQL After Insert在数据库恢复后未执行触发器

时间:2016-07-08 10:58:01

标签: sql sql-server triggers

我将SQL 2k5数据库恢复到SQL 2014服务器(注意这是一个SQL Web Edition实例),它似乎根本不执行。

CREATE TRIGGER [dbo].[tInsertTransactionsSnapshot]
ON  [GDL_TNA].[dbo].[TRANSACTIONS] 
AFTER INSERT
AS 
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @FirstRecord int
DECLARE @LastRecord int
DECLARE @MSTSQ int
DECLARE @EventDate varchar(50)
SET @FirstRecord = (SELECT TOP 1 T_ID FROM INSERTED ORDER BY T_ID ASC)
SET @LastRecord = (SELECT TOP 1 T_ID FROM INSERTED ORDER BY T_ID DESC)

insert into GDL_TNA.dbo.dbg(Dbgfirst,dbgLast) VALUES (@FirstRecord,@LastRecord)

WHILE @FirstRecord <= @LastRecord
BEGIN
    SET @MSTSQ = (SELECT MST_SQ FROM INSERTED WHERE T_ID = @FirstRecord)
    SET @EventDate = (SELECT T_Date FROM INSERTED WHERE T_ID = @FirstRecord)

    IF EXISTS(SELECT NULL FROM GDL_TNA.dbo.SNAPSHOTs WHERE MST_SQ = @MSTSQ AND SS_EVENT_DATE = @EventDate AND SS_ACTIVE = 1 AND SS_TYPE = 'TRANSACTION')
    BEGIN
        UPDATE GDL_TNA.dbo.SNAPSHOTs SET SS_ACTIVE = 0 WHERE MST_SQ = @MSTSQ AND SS_EVENT_DATE = @EventDate AND SS_ACTIVE = 1 AND SS_TYPE = 'TRANSACTION'
    END

    INSERT INTO GDL_TNA.dbo.SNAPSHOTs
    (
         [SS_TYPE]
        ,[SS_EVENT]
        ,[MST_SQ]
        ,[MD_ACTIVE]
        ,[MST_FIRST_NAME]
        ,[MST_LAST_NAME]
        ,[MD_SHIFTSTART]
        ,[MD_SHIFTEND]
        ,[COMP_ID]
        ,[DEPT_ID]
        ,[FUNC_ID]
        ,[MST_PREVIOUS_FIRST_NAME]
        ,[MST_PREVIOUS_LAST_NAME]
        ,[MST_RATE_NORMAL]
        ,[MST_RATE_TIMEANDHALF]
        ,[MST_RATE_DOUBLE]
        ,[MST_RATE_SPECIAL]
        ,[SS_EVENT_ID]
        ,[SS_EVENT_DATE]
        ,[SS_EVENT_START]
        ,[SS_EVENT_END]
        ,[SS_EVENT_APPROVED]
        ,[SS_EVENT_RATETYPE]
    )
    SELECT
         'TRANSACTION'
        ,'INSERT'
        ,MST_SQ
        ,MD_ACTIVE
        ,MST_FIRST_NAME
        ,MST_LAST_NAME
        ,MD_SHIFTSTART
        ,MD_SHIFTEND
        ,COMP_ID
        ,DEPT_ID
        ,FUNC_ID
        ,MST_PREVIOUS_FIRST_NAME
        ,MST_PREVIOUS_LAST_NAME
        ,MST_RATE_NORMAL
        ,MST_RATE_TIMEANDHALF
        ,MST_RATE_DOUBLE
        ,MST_RATE_SPECIAL
        ,(
            SELECT T_ID
            FROM INSERTED
            WHERE T_ID = @FirstRecord
         )
        ,(
            SELECT T_Date
            FROM INSERTED
            WHERE T_ID = @FirstRecord
         )
        ,ISNULL((
            SELECT  
                CASE
                    WHEN len(T_TIMEIN) = 6 THEN left(T_TIMEIN,2) + ':' + right(left(T_TIMEIN,4),2) + ':' + right(T_TIMEIN,2) + '.000'
                    WHEN len(T_TIMEIN) < 6 THEN '0'+left(T_TIMEIN,1) + ':' + right(left(T_TIMEIN,3),2) + ':' + right(T_TIMEIN,2) + '.000'
                END
            FROM INSERTED
            WHERE T_ID = @FirstRecord
         ),'00:00:00.000')           
        ,ISNULL((
            SELECT 
                CASE
                    WHEN len(T_TIMEOUT) = 6 THEN left(T_TIMEOUT,2) + ':' + right(left(T_TIMEOUT,4),2) + ':' + right(T_TIMEOUT,2) + '.000'
                    WHEN len(T_TIMEOUT) < 6 THEN '0'+left(T_TIMEOUT,1) + ':' + right(left(T_TIMEOUT,3),2) + ':' + right(T_TIMEOUT,2) + '.000'
                END
            FROM INSERTED
            WHERE T_ID = @FirstRecord
         ),'00:00:00.000')
        ,1
        ,1
    FROM GDL_TNA.dbo.MASTER_DETAILS
    WHERE MST_SQ = @MSTSQ

    SET @FirstRecord = @FirstRecord + 1
END
END

我采取的步骤

  1. Check Trigger Status SELECT name,is_disabled FROM sys.triggers (全部启用)
  2. Drop&amp;在2014年实例上重新创建触发器
  3. 使用 sp_recompile
  4. 重新编译表格

    非常感谢任何建议。

0 个答案:

没有答案