我一直在研究这个触发器,但没能弄清楚。我得到了这里的一些人的帮助,取得了很大的进步,但还没有完成。
这里是我的触发器与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
试试这个-
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