我有两个表,PatientMedicalHistory和PatientMedicineTable。患者病史有以下几个方面 -
PatientMedicalHistoryID Primary Key for PatientMedical History Table
Medications Boolean for existence of prior medications
Allergies Boolean for existence of allergies
Diseases Boolean for existence of prior diseases
FamilyHistory Boolean for existence of hereditary conditions
Patient Medicine Table历史记录包含以下字段 -
PatientMedicalHistoryID Primary, Foreign Key for Patient Table
MedicineID Primary, Foreign Key for Medicine Table
当条目插入Patient Medicine表时,有没有办法创建一个触发器来更新Medications布尔值?
AFTER INSERT触发器应该是您正在寻找的。下面的链接是关于创建它的教程。 http://www.sqlservertutorial.net/sql-server-triggers/sql-server-create-trigger/
正如您在问题中所述,这可以通过triggers完成。具体来说,你对AFTER INSERT
触发器感兴趣。您的代码大致如下所示
-- recreating the trigger to be sure
DROP TRIGGER [dbo].[YourTrigger]
GO
CREATE TRIGGER [dbo].[YourTrigger]
ON [dbo].[PatientMedicineTable]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;
UPDATE h
SET h.[Medications] = 1
FROM Inserted i, [dbo].[PatientMedicalHistory] h
WHERE h.PatientMedicalHistoryID = i.PatientMedicalHistoryID
END
GO
请注意AFAIK您通过1或0设置位字段来处理SQL Server中的布尔值,但您可能需要另一个选项。