当插入发生在另一个表上时,是否有一个存储过程来更新一个表中的布尔值?

问题描述 投票:0回答:2

我有两个表,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布尔值?

sql sql-server stored-procedures database-trigger
2个回答
0
投票

AFTER INSERT触发器应该是您正在寻找的。下面的链接是关于创建它的教程。 http://www.sqlservertutorial.net/sql-server-triggers/sql-server-create-trigger/


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中的布尔值,但您可能需要另一个选项。

© www.soinside.com 2019 - 2024. All rights reserved.