我在数据库上设置了以下触发器。它确定是否已依次从等候名单中删除记录, 然后在表中插入一条记录,如果记录被依次删除则将记录标记为1,如果没有按照等候名单中的正确顺序删除记录则为0。
当我通过管理工作室使用我的管理员帐户手动将记录插入表中时,触发器运行正常。但是,它仅使用我的通用登录从应用程序执行,当以下两行被注释掉时:
" IF EXISTS(SELECT * FROM inserted)"和"和co.RecordNum(SELECT RecordNum FROM FROM)"
我在过去几天研究了这个,我还没有找到解决方案。我想知道是否有人能告诉我为什么会这样,以及如何解决这个问题?
ALTER TRIGGER [dbo].[trgIsWLRemovalInTurn] ON [dbo].[CAREOFFER]
WITH EXECUTE AS OWNER
AFTER INSERT
AS
BEGIN
IF @@ROWCOUNT = 0
RETURN
SET NOCOUNT ON
IF EXISTS(SELECT * FROM inserted)
INSERT INTO dbo.tmpWaitlistInTurn
SELECT code as DR#, wl.RecordNum, wl.dtListed, wl.lPosition, wl.waitlist, wl.wStatus,
wl.[description], wl.[2ndSubClass], wl.dtRemoval, co.dtOffered,
wl.clinic, wl.wPriority, wl.episodeID, wl.patSurname, wl.ridCareOffer,
CASE WHEN wl.dtListed > (SELECT TOP 1 dtListed
FROM WAITLIST wl1
WHERE wstatus = 0
and dtListed is not null
and Deleted = 0
and waitlist = wl.waitlist
ORDER BY dtListed ASC) THEN 1 ELSE 0 END AS RemovedInTurn
FROM WAITLIST wl
join DEBTOR4 d4 ON wl.ridPatient = d4.RecordNum and d4.Deleted = 0
join CAREOFFER co ON co.RecordNum = wl.ridCareOffer and co.Deleted = 0
WHERE wl.Deleted = 0
and wl.wStatus <> 0
and co.RecordNum in (SELECT RecordNum FROM inserted)
ORDER BY waitlist, wl.dtListed DESC
END
GO