为 INSERT 语句绑定触发器 insert virtual table
Binding trigger insert virtual table for INSERT statement
我有一个触发器可以毫无问题地将一些数据插入 table。问题是在同一个触发器中,我还需要更新或插入一个额外的 table ,具体取决于匹配记录是否已存在。我想我已经正确编码了 UPDATE 部分,但是我对如何执行 INSERT 感到困惑。我收到错误 The multi-part identifier "inserted.PatientId" could not be bound
ALTER TRIGGER [trg_Patient_InsertUpdate]
ON [Patient]
AFTER INSERT, UPDATE
AS
BEGIN
-- This inserts data into the PatientDiagnosis table
-- regardless of the DiagnosisStatus
INSERT INTO PatientDiagnosis
(
PatientId,
PhysicianID,
AdmissionYear,
DiagnosisStatus
)
SELECT
inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,
inserted.DiagnosisStatus
FROM INSERTED
-- Here I want to UPDATE a second table "PatientTracking"
-- if the column "DiagnosisStatus" = 1
UPDATE pt
SET pt.DiagnosisReady = 1,
pt.Report = 'PENDING'
FROM PatientTracking as pt
INNER JOIN INSERTED AS i
ON pt.PatientId= i.[PatientId]
AND pt.PhysicianID= i.[PhysicianID]
AND pt.AdmissionYear= i.[AdmissionYear]
WHERE i.DiagnosisStatus = 1
-- If the record does not exist in "PatientTracking" then INSERT
-- This is where the error is coming from
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
VALUES (inserted.[PatientId], inserted.[PhysicianID], inserted.[AdmissionYear], 1)
END
END
INSERT 语句是我出错的地方 The multi-part identifier "inserted.PatientId" could not be bound
。如何让 INSERT 语句识别 inserted
虚拟 table?
注意 * - 我知道 MERGE
语句可用,但根据我的经验,它仍然存在一些错误,特别是它会干扰 Entity Framework 绑定。
试试这个,使用相同的 select 语句,
-- This inserts data into the PatientDiagnosis table
-- regardless of the DiagnosisStatus
INSERT INTO PatientDiagnosis
(
PatientId,
PhysicianID,
AdmissionYear,
DiagnosisStatus
)
SELECT
inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,
inserted.DiagnosisStatus
FROM INSERTED
-- Here I want to UPDATE a second table "PatientTracking"
-- if the column "DiagnosisStatus" = 1
UPDATE pt
SET pt.DiagnosisReady = 1,
pt.Report = 'PENDING'
FROM PatientTracking as pt
INNER JOIN INSERTED AS i
ON pt.PatientId= i.[PatientId]
AND pt.PhysicianID= i.[PhysicianID]
AND pt.AdmissionYear= i.[AdmissionYear]
WHERE i.DiagnosisStatus = 1
-- If the record does not exist in "PatientTracking" then INSERT
-- This is where the error is coming from
IF NOT EXISTS(SELECT 1 FROM PatientTracking
INNER JOIN inserted
ON inserted.PatientId = PatientTracking.PatientId
)
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
SELECT inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,1
FROM INSERTED
END
试试这个 SQL FIDDLE http://sqlfiddle.com/#!3/61e4f5/3
您的代码正确率为 99.99%。只需更改插入语句,以便在查询中实际插入 table。您没有在插入中引用 table,因此它不是查询的一部分。
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
select i.[PatientId], i.[PhysicianID], i.[AdmissionYear], 1
from inserted i
END
我有一个触发器可以毫无问题地将一些数据插入 table。问题是在同一个触发器中,我还需要更新或插入一个额外的 table ,具体取决于匹配记录是否已存在。我想我已经正确编码了 UPDATE 部分,但是我对如何执行 INSERT 感到困惑。我收到错误 The multi-part identifier "inserted.PatientId" could not be bound
ALTER TRIGGER [trg_Patient_InsertUpdate]
ON [Patient]
AFTER INSERT, UPDATE
AS
BEGIN
-- This inserts data into the PatientDiagnosis table
-- regardless of the DiagnosisStatus
INSERT INTO PatientDiagnosis
(
PatientId,
PhysicianID,
AdmissionYear,
DiagnosisStatus
)
SELECT
inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,
inserted.DiagnosisStatus
FROM INSERTED
-- Here I want to UPDATE a second table "PatientTracking"
-- if the column "DiagnosisStatus" = 1
UPDATE pt
SET pt.DiagnosisReady = 1,
pt.Report = 'PENDING'
FROM PatientTracking as pt
INNER JOIN INSERTED AS i
ON pt.PatientId= i.[PatientId]
AND pt.PhysicianID= i.[PhysicianID]
AND pt.AdmissionYear= i.[AdmissionYear]
WHERE i.DiagnosisStatus = 1
-- If the record does not exist in "PatientTracking" then INSERT
-- This is where the error is coming from
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
VALUES (inserted.[PatientId], inserted.[PhysicianID], inserted.[AdmissionYear], 1)
END
END
INSERT 语句是我出错的地方 The multi-part identifier "inserted.PatientId" could not be bound
。如何让 INSERT 语句识别 inserted
虚拟 table?
注意 * - 我知道 MERGE
语句可用,但根据我的经验,它仍然存在一些错误,特别是它会干扰 Entity Framework 绑定。
试试这个,使用相同的 select 语句,
-- This inserts data into the PatientDiagnosis table
-- regardless of the DiagnosisStatus
INSERT INTO PatientDiagnosis
(
PatientId,
PhysicianID,
AdmissionYear,
DiagnosisStatus
)
SELECT
inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,
inserted.DiagnosisStatus
FROM INSERTED
-- Here I want to UPDATE a second table "PatientTracking"
-- if the column "DiagnosisStatus" = 1
UPDATE pt
SET pt.DiagnosisReady = 1,
pt.Report = 'PENDING'
FROM PatientTracking as pt
INNER JOIN INSERTED AS i
ON pt.PatientId= i.[PatientId]
AND pt.PhysicianID= i.[PhysicianID]
AND pt.AdmissionYear= i.[AdmissionYear]
WHERE i.DiagnosisStatus = 1
-- If the record does not exist in "PatientTracking" then INSERT
-- This is where the error is coming from
IF NOT EXISTS(SELECT 1 FROM PatientTracking
INNER JOIN inserted
ON inserted.PatientId = PatientTracking.PatientId
)
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
SELECT inserted.PatientId,
inserted.PhysicianID,
inserted.AdmissionYear,1
FROM INSERTED
END
试试这个 SQL FIDDLE http://sqlfiddle.com/#!3/61e4f5/3
您的代码正确率为 99.99%。只需更改插入语句,以便在查询中实际插入 table。您没有在插入中引用 table,因此它不是查询的一部分。
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady)
select i.[PatientId], i.[PhysicianID], i.[AdmissionYear], 1
from inserted i
END