SQL触发器错误 -

时间:2015-05-28 18:10:11

标签: sql-server triggers

我正在尝试开发一个触发器,但我似乎遇到了以下错误:

  

名称为' CONTACT_CURSOR'的光标不存在。 5339B6CC-5480-4B24-AC5D-3A4C0EAF64EC 3F0F445B-7BA3-4311-944A-E372D41FD307声明已终止。

这是我正在努力开发的触发器:

USE [124959test]
GO
/****** Object:  Trigger [dbo].[TRG_AUTOCAMPAIGN_Based_on_IDStatus_Value_Latest]    Script Date: 05/28/2015 10:48:50 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[TRG_AUTOCAMPAIGN_Based_on_IDStatus_Value_Latest]
   ON  [dbo].[SN_Contact2]
   FOR INSERT, UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    IF (UPDATE(contact_category))  -- selector field is the one that determines if an action shou dbe taken
    BEGIN
        DECLARe @CAMPAIGNSTARTTIME datetime
        DECLARE @DELETEOLD INT
        DECLARE @TRIGGERFIELD VARCHAR(100)
        DECLARE @USERID VARCHAR(36)
        DECLARE @CONTACTID VARCHAR(36)
        DECLARE @CAMPAIGNID VARCHAR(36)
        DECLARE @SCHEDULEFOR VARCHAR(36)
        DECLARE @COMMINGLE VARCHAR(100)
        DECLARE @DROPSHIP VARCHAR(100)
        DECLARE @EDDM VARCHAR(100)
        DECLARE @EXPEDITEDDROPSHIP VARCHAR(100)
        DECLARE @EXPEDITEDSERVICES VARCHAR(100)
        DECLARE @FIRSTCLASS VARCHAR (100)
        DECLARE @FLATS VARCHAR(100)
        DECLARE @FREIGHTROVER VARCHAR(100)
        DECLARE @INTERNATIONAL VARCHAR(100)
        DECLARE @MAILROVER VARCHAR(100)
        DECLARE @OBBASHIP VARCHAR(100)
        DECLARE @OBBATRACK VARCHAR(100)
        DECLARE @PARCEL VARCHAR(100)
        DECLARE @PRODUCTION VARCHAR(100)

        SELECT @DELETEOLD = 0

        DECLARE CONTACT_CURSOR CURSOR FOR
            SELECT Contact_ContactID, SNL_RecordManagerID,contact_category,cust_commingle,CUST_Drop_Ship,
            cust_eddm,CUST_Expedited_Drop_Ship,CUST_Expedited_Services,CUST_First_Class,CUST_Flats,
            CUST_Freight_Rover,CUST_International,CUST_MailRover,CUST_OBBAShip,CUST_OBBATrack,
            CUST_Parcel,CUST_Production FROM inserted
            --where snl_recordmanagerid = '1C42B435-4A81-4109-A008-34AE91C2D48F'

        OPEN CONTACT_CURSOR
        FETCH NEXT FROM CONTACT_CURSOR 
            INTO @CONTACTID, @USERID, @TRIGGERFIELD, @COMMINGLE, @DROPSHIP, @EDDM, @EXPEDITEDDROPSHIP, @EXPEDITEDSERVICES,
             @FIRSTCLASS, @FLATS, @FREIGHTROVER, @INTERNATIONAL, @MAILROVER, @OBBASHIP, @OBBATRACK, @PARCEL, @PRODUCTION

        WHILE @@FETCH_STATUS = 0
        BEGIN
            IF @TRIGGERFIELD = 'Client' AND @USERID = '5339B6CC-5480-4B24-AC5D-3A4C0EAF64EC'
            --or @USERID = '277D668E-F18F-48E7-B7C1-2FDCF50F1B36'
        --or @USERID = '7FF6E867-8DDA-47FE-9A2F-E7E042FFAC56'
            /* This is to launch the New Client Workflow*/
            BEGIN
                SET @CAMPAIGNID = 'D25053D9-EF07-4184-98F5-EC6DB1814B7E' -- CampaignID of the New client Campaign
                SET @Campaignstarttime = getdate()
                EXEC usp_AssignCampaign @CONTACTID, @USERID, @CAMPAIGNID, @DELETEOLD, @CAMPAIGNSTARTTIME
            /* This is to launch the Send Welcome Package Workflow*/
            SET @CAMPAIGNID = '08118AC6-456B-4BEC-BD09-F09A0FDC893D' -- CampaignID of the Welcome Package Campaign.
            SET @Campaignstarttime = getdate()
            SET @USERID = '9E283EF8-208F-4A60-BACA-555F3BECEBAB' -- The UserID of to whom the to-do task will be scheduled to.
            EXEC usp_AssignCampaign @CONTACTID, @USERID, @CAMPAIGNID, @DELETEOLD, @CAMPAIGNSTARTTIME

            END

            ELSE
            BEGIN


            IF @TRIGGERFIELD = 'Prospect'
            BEGIN
                --Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
                SET @CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
                SET @Campaignstarttime = getdate()
                EXEC usp_AssignCampaign @CONTACTID, @USERID, @CAMPAIGNID, @DELETEOLD, @CAMPAIGNSTARTTIME

            END

            ELSE
            BEGIN

              IF @TRIGGERFIELD = 'Hot Prospect'
            BEGIN
                --Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
                SET @CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
                SET @Campaignstarttime = getdate()
                EXEC usp_AssignCampaign @CONTACTID, @USERID, @CAMPAIGNID, @DELETEOLD, @CAMPAIGNSTARTTIME
            end
            end

            FETCH NEXT FROM CONTACT_CURSOR 
                INTO @CONTACTID, @USERID, @TRIGGERFIELD, @COMMINGLE, @DROPSHIP, @EDDM, @EXPEDITEDDROPSHIP, @EXPEDITEDSERVICES,
             @FIRSTCLASS, @FLATS, @FREIGHTROVER, @INTERNATIONAL, @MAILROVER, @OBBASHIP, @OBBATRACK, @PARCEL, @PRODUCTION
        END

        CLOSE CONTACT_CURSOR
        DEALLOCATE CONTACT_CURSOR
    END
end
end

有什么想法吗?

3 个答案:

答案 0 :(得分:0)

我认为你在错误的地方有 END 。它正在推动你的Fetch Next超出你的Cursor声明的范围。尝试将代码的结尾修改为以下内容......

                BEGIN
                        --Update sn_contact2 set cust_newsletter = Yes' -- When ID/Status = Prospect/Hot Prospect update Newsletter field to Yes.
                        SET @CAMPAIGNID = 'B1649041-FB74-40D1-807C-F969D8AE4D40' -- CampaignID of the Opt-out Email with LinkedIn Follow Up Campaign
                        SET @Campaignstarttime = getdate()
                        EXEC usp_AssignCampaign @CONTACTID, @USERID, @CAMPAIGNID, @DELETEOLD, @CAMPAIGNSTARTTIME
                    end
                end
            END 
            FETCH NEXT FROM CONTACT_CURSOR 
                INTO @CONTACTID, @USERID, @TRIGGERFIELD, @COMMINGLE, @DROPSHIP, @EDDM, @EXPEDITEDDROPSHIP, @EXPEDITEDSERVICES,
             @FIRSTCLASS, @FLATS, @FREIGHTROVER, @INTERNATIONAL, @MAILROVER, @OBBASHIP, @OBBATRACK, @PARCEL, @PRODUCTION

        END
        CLOSE CONTACT_CURSOR
        DEALLOCATE CONTACT_CURSOR
    END
end

答案 1 :(得分:0)

代码有几个问题。首先,您需要在ENDBEGIN之前移动FETCH WHILE。接下来你需要将CLOSEDEALLOCATE移到外面。

                        FETCH NEXT FROM CONTACT_CURSOR 
                        INTO @CONTACTID, @USERID, @TRIGGERFIELD, @COMMINGLE, @DROPSHIP, @EDDM, @EXPEDITEDDROPSHIP,
                        @EXPEDITEDSERVICES, @FIRSTCLASS, @FLATS, @FREIGHTROVER, @INTERNATIONAL, @MAILROVER,
                        @OBBASHIP, @OBBATRACK, @PARCEL, @PRODUCTION
                END
            CLOSE CONTACT_CURSOR
            DEALLOCATE CONTACT_CURSOR
        END
END

这是光标的基本轮廓

DECLARE Some_Cursor CURSOR

OPEN Some_Cursor
FETCH NEXT FROM Some_Cursor

WHILE @@FETCH_STATUS = 0
    BEGIN 

        FETCH NEXT FROM Some_Cursor
    END

CLOSE Some_Cursor
DEALLOCATE Some_Cursor

答案 2 :(得分:0)

感谢所有帮助人员!我已经使这个触发器运行,在我验证它正常工作之后,我将对其进行优化或重组,以便它不再使用联系游标。

感谢帮助!

再次感谢!

-Mike