绑定触发器为INSERT语句插入虚拟表

时间:2015-01-05 21:18:36

标签: sql sql-server

我有一个触发器,可以毫无问题地将一些数据插入到表中。问题是在同一个触发器中我还需要更新或插入一个额外的表,具体取决于匹配的记录是否已经存在。我认为我已正确编码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虚拟表?

注意* - 我知道可用的MERGE语句但我的经验是它仍然存在一些错误,特别是它会干扰实体框架绑定。

2 个答案:

答案 0 :(得分:1)

试试这个,使用相同的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

答案 1 :(得分:1)

您的代码正确率为99.99%。只需更改插入语句,以便在查询中实际包含插入的表。您没有在插入中引用该表,因此它不是查询的一部分。

IF @@ROWCOUNT = 0
    BEGIN
        INSERT INTO PatientTracking (PatientId, PhysicianID, AdmissionYear, DiagnosisReady) 
        select i.[PatientId], i.[PhysicianID], i.[AdmissionYear], 1
        from inserted i
    END