0

我一直在研究这个触发器,但无法弄清楚。我从这里的人那里得到了一些帮助,取得了很大的进步,但还没有完成。

这是我加入的触发器。插入没有发生....我无法调试,也看不到错误(如果有)。不幸的是,我正在使用 SQL 网络工具

CREATE trigger Posts_Raw_To_Queue_Trigger  ON SendNotificationPostsRaw 
FOR INSERT
AS
BEGIN
  INSERT INTO SendNotificationPostsQueue (UserID,PostID,SpecialityID)
  SELECT I.PostID, I.UserID, P.CategoryId   
  FROM INSERTED AS I JOIN PostCategoryRelations AS P ON I.PostID= P.PostId
END
4

1 回答 1

1

试试这个——

CREATE TRIGGER dbo.Posts_Raw_To_Queue_Trigger

    ON dbo.SendNotificationPostsRaw

    -- please write this if SendNotificationPostsRaw is table 
    AFTER INSERT
    -- or write this if SendNotificationPostsRaw is view
    INSTEAD OF INSERT

    --FOR INSERT

AS BEGIN

    -- check if thete are any rows
    IF NOT EXISTS(
        SELECT 1 
        FROM INSERTED i
    ) RAISERROR('Nothing to insert', 16, 1)


    INSERT INTO dbo.SendNotificationPostsQueue 
    (
          UserID
        , PostID
        , SpecialityID
    )
    SELECT  
          I.PostID
        , I.UserID
        , P.CategoryID
    FROM INSERTED AS I
    JOIN PostCategoryRelations P ON I.PostID = P.PostId

END
于 2013-04-12T05:54:10.147 回答