Sql触发器在运行时卡住数据库

时间:2015-11-19 09:37:08

标签: sql-server triggers

执行此触发后,数据库会卡住。我无法继续下去。

ALTER TRIGGER [dbo].[TRIG_INS_UPD_BATCH_HISTORY_ON_BATCH_STATUS_CHANGE]
ON [dbo].[PIM_BATCH] AFTER UPDATE
AS


    DECLARE @RECORDID bigint,
            @SYS_DATE datetime


    BEGIN
        PRINT ' TRIG_INS_UPD_BATCH_HISTORY_ON_BATCH_STATUS_CHANGE '
        IF UPDATE(BATCH_STATUS)

            BEGIN
            SELECT @SYS_DATE = CURRENT_TIMESTAMP

            DECLARE productids cursor for
             SELECT RECORD_ID FROM PIM_BATCH_WORK_ITEMS pbw WHERE 
             pbw.BATCH_ID=(SELECT pb.BATCH_ID FROM PIM_BATCH pb INNER JOIN deleted d 
             ON pb.BATCH_ID=d.BATCH_ID WHERE pb.BATCH_STATUS != d.BATCH_STATUS)

            open  productids

            fetch next from productids into @RECORDID

            WHILE ( @@FETCH_STATUS = 0 )

                BEGIN

                PRINT ' ITERATING IN PRODUCTIDS CURSOR ' + convert(varchar(20), @RECORDID)

                INSERT INTO PIM_BATCH_HISTORY (BATCH_ID,NEW_STATUS,OLD_STATUS,PRODUCT_ID,UPDATED_TIME)SELECT i.BATCH_ID,i.BATCH_STATUS,d.BATCH_STATUS, @RECORDID, @SYS_DATE FROM PIM_BATCH i INNER JOIN deleted d ON i.BATCH_ID = d.BATCH_ID 
                    WHERE i.BATCH_STATUS != d.BATCH_STATUS

                fetch next from productids into @RECORDID

                END

                PRINT ' CLOSING CURSOR PRODUCTIDS'

                close productids

                deallocate productids

            END

    END

0 个答案:

没有答案