我有两个表,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
“患者药物表”历史记录包含以下字段-
PatientMedicalHistoryID Primary, Foreign Key for Patient Table
MedicineID Primary, Foreign Key for Medicine Table
当将条目插入“患者药物”表中时,是否有一种方法可以创建触发器来更新“药物”布尔值?
答案 0 :(得分:0)
AFTER INSERT触发器应该是您想要的。以下链接是有关创建它的教程。 http://www.sqlservertutorial.net/sql-server-triggers/sql-server-create-trigger/
答案 1 :(得分:0)
正如您在问题中已经提到的那样,可以通过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中的布尔值,但是您可能需要其他选择。