0

我在触发器内创建了一个光标,但它无法正常工作。请帮我修复它

Create trigger Posts_Raw_To_Queue_Trigger  ON SendNotificationPostsRaw FOR INSERT
AS
BEGIN

DECLARE @PostID uniqueidentifier  
DECLARE @UserID uniqueidentifier
DECLARE @ProfID int
DECLARE @Email nvarchar(100)
DECLARE @CreationTime  datetime
DECLARE @SpecialityID int

SELECT @ProfID= ProfessionalID,@Email= Email from Professionals where UserID=@UserID
SELECT @PostID = I.PostID,@UserID = I.UserID ,@CreationTime =I.CreationTime  FROM INSERTED I

DECLARE post_relation_cursor CURSOR FOR select CategoryId  from PostCategoryRelations  where PostId=@PostID;

OPEN post_relation_cursor;
FETCH NEXT FROM post_relation_cursor INTO @SpecialityID 
WHILE @@FETCH_STATUS = 0  
BEGIN  
       INSERT INTO SendNotificationPostsQueue (UserID,PostID,SpecialityID,TemplateID,CreationTime,SendTime,JSONParameters) Values(@UserID,@PostID,1,1,'04/11/2013','04/11/2013','')

       FETCH NEXT FROM post_relation_cursor INTO @SpecialityID;
END;
CLOSE post_relation_cursor;
DEALLOCATE post_relation_cursor;

END

如果我删除光标并将虚拟值插入SendNotificationPostsQueue,它可以工作。所以我的光标有问题...请告诉我为什么光标不起作用?

4

2 回答 2

3

看来您根本不需要使用光标,并且几乎在所有情况下都不使用光标会更好。只需用标准插件替换触发器的主体(开始和结束之间的部分):

INSERT INTO SendNotificationPostsQueue (UserID,PostID,SpecialityID,TemplateID,CreationTime,SendTime,JSONParameters)
SELECT
    i.UserID,
    i.PostID,
    1,
    1,
    '04/11/2013', -- Might want i.CreationTime or current_timestamp
    '04/11/2013',
    ''
FROM INSERTED i
    -- possibly want "LEFT JOIN Professionals p on i.UserID = p.UserID" here to grab other info

请注意我没有使用values只能插入一行的插入子句。我将选择语句作为插入的一部分,因此插入的行数与选择返回的行数一样多。这意味着我们不需要使用游标,也不需要一堆变量来为游标提供动力。

正如@automatic 所提到的,您当前代码的一个问题是您假设 INSERTED 仅保留一行。如果它有多个,那么当您尝试将列分配给变量时会引发错误。

出于优雅、可维护性和性能的原因,我强烈建议您放弃此游标并运行一个简单的插入(因为这就是您的游标所做的一切)。

于 2013-04-11T12:45:24.180 回答
1

可能的问题在这里 -

CREATE TRIGGER dbo.Posts_Raw_To_Queue_Trigger 

    ON SendNotificationPostsRaw
    -- for view
    INSTEAD OF INSERT
    -- OR
    -- for table
    AFTER INSERT

AS BEGIN

    DECLARE 
          @PostID UNIQUEIDENTIFIER
        , @UserID UNIQUEIDENTIFIER
        , @ProfID INT
        , @Email NVARCHAR(100)
        , @CreationTime DATETIME
        , @SpecialityID INT

    SELECT  @ProfID = ProfessionalID
        ,   @Email = Email
    FROM Professionals
    WHERE UserID = @UserID

    -- this posible return invalid result (random record from inserted sequence)
    SELECT  @PostID = I.PostID
        ,   @UserID = I.UserID
        ,   @CreationTime = I.CreationTime
    FROM INSERTED I

    DECLARE post_relation_cursor CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
        SELECT CategoryID
        FROM dbo.PostCategoryRelations
        WHERE PostId = @PostID;

    OPEN post_relation_cursor;

    FETCH NEXT FROM post_relation_cursor INTO @SpecialityID

    WHILE @@FETCH_STATUS=0 BEGIN

        INSERT INTO SendNotificationPostsQueue (
                UserID
            ,   PostID
            ,   SpecialityID
            ,   TemplateID
            ,   CreationTime
            ,   SendTime
            ,   JSONParameters
        )
        SELECT
                 @UserID
             ,   @PostID
             ,   @SpecialityID --- !!!
             ,   1
             ,   '04/11/2013'
             ,   '04/11/2013'
             ,   ''


        FETCH NEXT FROM post_relation_cursor INTO @SpecialityID;

    END;

    CLOSE post_relation_cursor;
    DEALLOCATE post_relation_cursor;

END

更新

如果我理解正确的话,业务逻辑一定是这样的:

CREATE TRIGGER dbo.Posts_Raw_To_Queue_Trigger 

    ON dbo.SendNotificationPostsRaw
    [INSTEAD OF]/[AFTER] INSERT

AS BEGIN

    SET NOCOUNT ON;

    DECLARE 
          @PostID UNIQUEIDENTIFIER
        , @UserID UNIQUEIDENTIFIER
        , @ProfID INT
        , @Email NVARCHAR(100)
        , @CreationTime DATETIME
        , @SpecialityID INT

    DECLARE cur CURSOR LOCAL READ_ONLY FAST_FORWARD FOR
        SELECT
              i.PostID
            , i.UserID
            , ProfID = p.ProfessionalID
            , p.Email
            , i.CreationTime
            , pcr.CategoryID
        FROM INSERTED i
        JOIN dbo.Professionals p ON i.UserID = p.UserID
        JOIN dbo.PostCategoryRelations pcr ON i.PostID = pcr.PostID

    OPEN cur

    FETCH NEXT FROM cur INTO 
          @PostID 
        , @UserID 
        , @ProfID 
        , @Email
        , @CreationTime 
        , @SpecialityID 

    WHILE @@FETCH_STATUS = 0 BEGIN

        INSERT INTO dbo.SendNotificationPostsQueue 
        (
              UserID
            , PostID
            , SpecialityID
            , TemplateID
            , CreationTime
            , SendTime
            , JSONParameters
        )
        SELECT
              @UserID
            , @PostID
            , @SpecialityID
            , 1
            , @CreationTime
            , @CreationTime
            , ''

        FETCH NEXT FROM cur INTO 
              @PostID 
            , @UserID 
            , @ProfID 
            , @Email
            , @CreationTime 
            , @SpecialityID 

    END

    CLOSE cur
    DEALLOCATE cur

END
于 2013-04-11T12:31:22.960 回答