sql update 觸發器 可獲得被update的行的資訊_MsSql

來源:互聯網
上載者:User
複製代碼 代碼如下:

create trigger TgName
on tb
for update
as
if update(recommend)
begin
update tb set commenddate=(getdate()) from tb inner join inserted on tb.vlistid=Inserted.vlistid
end

recommend表示被更新的欄位.
關鍵在於Inserted表
觸發器語句中使用了兩種特殊的表:deleted 表和 inserted 表。
Deleted 表用於儲存 DELETE 和 UPDATE 語句所影響的行的複本。在執行 DELETE 或 UPDATE 語句時,行從觸發器表中刪除,並傳輸到 deleted 表中。Deleted 表和觸發器表通常沒有相同的行。

Inserted 表用於儲存 INSERT 和 UPDATE 語句所影響的行的副本。在一個插入或更新交易處理中,建立行被同時添加到 inserted 表和觸發器表中。Inserted 表中的行是觸發器表中新行的副本。

1.插入操作(Insert)
Inserted表有資料,Deleted表無資料

2.刪除操作(Delete)
Inserted表無資料,Deleted表有資料

3.更新操作(Update)
Inserted表有資料(新資料),Deleted表有資料(舊資料)

生前何必久睡,死後自會長眠!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.