我有3個表tbl_Users
,tbl_Protocol
和tbl_ProtocolDetails
和我的觸發內部上Users
,我一定要插入到Protocol
然後插入ProtocolDetails
,但我不知道如何工作的插入範圍。如何從觸發器中獲取其他表中插入的ID?
類似的東西:
CREATE TRIGGER tg_Users ON tbl_Users
AFTER INSERT, UPDATE AS
BEGIN
DECLARE @UserId = Int
DECLARE @ProtocolId = Int
DECLARE @UserDetail = NVARCHAR(255)
SELECT
@UserId = user_id,
@UserDetail = user_detail + '@' + user_explanation
FROM INSERTED
INSERT INTO tbl_Protocol (user_id, inserted_date)
VALUES (@UserId, GetDate())
-- Return Inserted Id from tbl_Protocol into @ProtocolDetail then
INSERT INTO tbl_ProtocolDetails (protocol_id, protocol_details)
VALUES (@ProtocolId, @UserDetail)
END
謝謝你的幫助和解釋,我不知道這個數據庫有這個行爲 –