2

患者と診断という 2 つのテーブルがあります。次のように

患者の診断
---------- ------------
ID(PK) ID(PK)
Name PatientID (FK: 患者への参照 => ID)
状態
****** *****
---------- -----------

ここで、患者のステータスは [登録済み診断済み、コース中]の場合があります。

その間、

  • 新しい患者挿入物、患者ステータスが登録されます
  • 新しい診断挿入物、患者の状態が診断されます

ここで、 診断の削除時に、患者が診断テーブルに少なくとも 1 つの診断エントリを持っているかどうかを確認する必要があります。それ以外の場合は、患者のステータス診断されます。

では、シングルトリガーでこれらすべての条件を実行するにはどうすればよいでしょうか?

私を助けてください。

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

私は実際にこれを2つのトリガーとして実行したでしょう。の 1AFTER DELETEつと の 1 つAFTER INSERTこれは、INSERT がある場合にDELETEコードが実行されないことを意味し、逆もまた同様です。しかし、上記のコードは実際に正しく動作します。

編集

ニコラが発見したAS。同じ操作で、同じ患者に対して複数の診断が挿入または更新された場合、単一の患者レコードが複数回更新される可能性があります。これらの変更はそれに対処する必要があります...

    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

ステータスは StatusID である必要がありますが、適切な ID 番号について言及していません。

于 2012-05-17T08:57:19.180 に答える