![](/img/trans.png)
[英]If null then insert in table_1 else return table_2 in sql server
[英]How to use SQL Server trigger to insert data into table_2(Log)
如何在更新表時創建觸發器,我希望table_2
從表transactions
transaction_id
扳機:
ALTER TRIGGER [dbo].[after_update]
ON [dbo].[transactions]
AFTER UPDATE
AS
BEGIN
INSERT INTO Table_2 (detail, transaction_id)
VALUES (GETDATE(), (SELECT transaction_id
FROM transactions
WHERE transaction_id = 3))
END
表:table_2(日志)
id detail transaction_id
------------------------------------------
20 Jan 16 2021 1:42AM NULL
表:交易:
id request_original transaction_id
------------------------------------------
1 NULL 842202611500000001
2 NULL 842202621200000001
3 1 842202621200000002
4 NULL 842202621200000003
5 NULL 842202621700000001
6 NULL 842202621700000002
我想你想要這個邏輯:
INSERT INTO Table_2 (detail, transaction_id)
SELECT GETDATE(), transaction_id
FROM inserted;
我不確定where transaction_id = 3
是什么。 但是,不涉及inserted
或deleted
的 SQL 服務器觸發器可能已損壞——假設它們只有一行的觸發器肯定已損壞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.