sql-server触发器连接不起作用

时间:2013-04-11 14:16:17

标签: sql sql-server insert left-join inner-join

我一直在研究这个触发器,但无法理解。我得到了一些人的帮助,并取得了很多进展,但还没有完成。

这是我加入的触发器。插入不会发生....我无法调试,如果有任何不能看到错误。我很遗憾地使用SQL Web工具

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

1 个答案:

答案 0 :(得分:0)

试试这个 -

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