2

我有 2 个表,称为 Patient 和 Diagnosis。如下

患者诊断
--------- ------------
身份 (PK) 身份 (PK)
姓名 PatientID(FK:参考患者 => ID)
地位
****** *****
---------- ------------

在这里,患者状态可能是 [ 已注册、已诊断OnCourse ]

中,

  • 新患者插入,患者状态将被注册
  • 新的诊断插入,将诊断患者状态

现在,在 诊断删除时,我需要检查如果患者在诊断表中至少有一个诊断条目,那么患者状态将被诊断,否则已注册

那么,如何在 Single Trigger 中完成所有这些条件呢?

请帮我。

4

2 回答 2

5

根据诊断表上的 INSERT 和 DELETE 触发更新 Patient.Status。

CREATE TRIGGER dbo.Diagnosis_TrgInsDel
  ON  dbo.Diagnosis
  AFTER DELETE, INSERT
AS 
BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;


    -- Change the Status to 'Registered' after the last
    -- Diagnosis record is deleted
    --------------------------------------------------------------------------------
    UPDATE
      Patient
    SET
      [Status] = 'Registered'
    FROM
      Patient
    INNER JOIN
      Deleted
        ON Deleted.PatientID = Patient.ID
    WHERE
      NOT EXISTS (SELECT * FROM Diagnosis WHERE PatientID = Deleted.PatientID)


    -- Change the Status to 'Diagnosed' after an Insert and the
    -- current Status is 'Registered'
    --------------------------------------------------------------------------------
    UPDATE
      Patient
    SET
      [Status] = 'Diagnosed'
    FROM
      Patient
    INNER JOIN
      Inserted
        ON Inserted.PatientID = Patient.ID
    WHERE
      Patient.[Status] = 'Registered'

END

我实际上会把它当作两个触发器。一为AFTER DELETE一为AFTER INSERT。这意味着当有 INSERT 时DELETE代码不会运行,反之亦然。但是上面的代码确实可以正常工作。

编辑

正如尼古拉发现的那样;如果在同一操作中为同一患者插入或更新多个诊断,则可能会多次更新单个患者记录。这些修改应解决...

    UPDATE
      Patient
    SET
      [Status] = 'Registered'
    WHERE
      NOT EXISTS (SELECT * FROM Diagnosis WHERE PatientID = Patient.ID)
      AND EXISTS (SELECT * FROM Deleted   WHERE PatientID = Patient.ID)

和...

    UPDATE
      Patient
    SET
      [Status] = 'Diagnosed'
    WHERE
          Patient.[Status] = 'Registered'
      AND EXISTS (SELECT * FROM Inserted WHERE PatientID = Patient.ID)
于 2012-05-17T09:13:16.880 回答
2

对于第一部分,您可以简单地添加默认约束:

alter table patient add constraint df_status default 'Registered' for status

如果这还不够,因为您的前端无法在插入中省略状态或将值设置为 DEFAULT,您可以创建一个触发器:

create trigger PatientInsertTrigger on patient
after insert
as
   -- trigger should not alter @@rowcount
   set nocount on

   update patient
      set status = 'Registered'
     from Patient
  -- Inserted is a pseudotable holding newly inserted rows
  -- This is how we know what records to update
    inner join Inserted
       on Patient.ID = Inserted.ID

在诊断中添加或删除记录时,应根据诊断中匹配记录的数量更新患者状态。幸运的是,在调用触发器时,记录已经在表中,因此在这两种情况下都使用 count() 就足够了。

create trigger DiagnosisTrigger on diagnosis
after insert, delete
as
   set nocount on

   update patient
      set status = case when d.NumberOfDiagnosis <> 0 
                        then 'Diagnosed'
                        else 'Registered'
                    end
     from Patient
    inner join
    (
      select PatientID, count(*) NumberOfDiagnosis
        from Diagnosis
       -- Get all patients with added or removed diagnosis
       where PatientID in (select PatientID 
                             from Inserted 
                            union 
                           select PatientID 
                           -- Pseudotable holding removed records
                             from Deleted)
       group by PatientID
    ) d
      on Patient.ID = d.PatientID

Status 应该是 StatusID,但您没有提及适当的 ID 号。

于 2012-05-17T08:57:19.180 回答