1. 程式人生 > 其它 >建立資料庫觸發器 基本操作方式

建立資料庫觸發器 基本操作方式

技術標籤:sql資料庫

USE [HengliReportWord]
GO
/****** Object:  Trigger [dbo].[CreateUpdates]    Script Date: 12/23/2020 11:14:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[CreateUpdates] --如果是建立就使用Create
on [dbo].[ReportMain]
after Update --這裡是操作狀態,其他狀態 有insert,delete 則相應的表操作進來的表是   updated,
insertd,delectd 在這裡找就可以 as begin declare @Id int; select top 1 @Id = Id from inserted order by CreateDate desc ;---在更新操作的表裡操作獲取操作資料的Id最後一條更新的資料 update ReportMain set updateSd = GETDATE() where Id=@Id end

注意問題:
狀態為:insert ,delete ,update
在 這個sql觸發中沒有 updated 這個表,一般是在deleted里加入表後再在Inserted里加一條。所以要獲取資料庫更新條目是insert 或者deleted 的最後一條