触发器记录 inserted/updated/deleted 值 SQL Server 2012
Trigger to log inserted/updated/deleted values SQL Server 2012
我正在使用 SQL Server 2012 Express,因为我真的很习惯 PL/SQL 所以很难找到我的 T-SQL 问题的答案。
我有: 大约 7 table 具有不同的列,另外一个用于记录来自其他 7.inserted/updated/deleted 的值。
问题: 我如何为每个 table 创建一个触发器,以便它将修改后的数据存储在 Log
table 上,考虑到我无法使用更改数据捕获,因为我使用的是 SQL Server Express 版本?
其他信息: Logs
table 中只有两列需要我帮助填写;来自合并的所有列的更改数据,示例如下:
CREATE TABLE USER_DATA
(
ID INT IDENTITY(1,1) NOT NULL,
NAME NVARCHAR2(25) NOT NULL,
PROFILE INT NOT NULL,
DATE_ADDED DATETIME2 NOT NULL
)
GO
CREATE TABLE AUDIT_LOG
(
ID INT IDENTITY(1,1) NOT NULL,
USER_ALTZ NVARCHAR(30) NOT NULL,
MACHINE SYSNAME NOT NULL,
DATE_ALTERERED DATETIME2 NOT NULL,
DATA_INSERTED XML,
DATA_DELETED XML
)
GO
我需要帮助填写的栏目是最后两栏(DATA_INSERTED
和 DATA_DELETED
)。我什至不确定数据类型是否应该是 XML,但是当有人
INSERTS
或 UPDATES
(仅限新值),USER_DATA
的 所有列 上的所有数据 inserted/updated 应该以某种方式合并到 DATA_INSERTED
.
DELETES
或 UPDATES
(仅限旧值),USER_DATA
的 所有列 上的所有数据 deleted/updated 应该以某种方式合并到 DATA_DELETED
.
可能吗?
Use the inserted and deleted Tables
DML trigger statements use two special tables: the deleted table and
the inserted tables. SQL Server automatically creates and manages
these tables. You can use these temporary, memory-resident tables to
test the effects of certain data modifications and to set conditions
for DML trigger actions. You cannot directly modify the data in the
tables or perform data definition language (DDL) operations on the
tables, such as CREATE INDEX. In DML triggers, the inserted and
deleted tables are primarily used to perform the following: Extend
referential integrity between tables. Insert or update data in base
tables underlying a view. Test for errors and take action based on the
error. Find the difference between the state of a table before and
after a data modification and take actions based on that difference.
和
Returns information from, or expressions based on, each row affected
by an INSERT, UPDATE, DELETE, or MERGE statement. These results can be
returned to the processing application for use in such things as
confirmation messages, archiving, and other such application
requirements. The results can also be inserted into a table or table
variable. Additionally, you can capture the results of an OUTPUT
clause in a nested INSERT, UPDATE, DELETE, or MERGE statement, and
insert those results into a target table or view.
只是 posting,因为这解决了我的问题。正如用户@SeanLange 在对我的 post 的评论中所说,他让我使用 "audit"
,我不知道它存在。
谷歌搜索,引导我到 this Whosebug answer 第一个 link 有一个创建触发器的过程和 "shadow" 表做我需要的事情(它没有合并所有值都放在一列中,但它适合工作)。
我正在使用 SQL Server 2012 Express,因为我真的很习惯 PL/SQL 所以很难找到我的 T-SQL 问题的答案。
我有: 大约 7 table 具有不同的列,另外一个用于记录来自其他 7.inserted/updated/deleted 的值。
问题: 我如何为每个 table 创建一个触发器,以便它将修改后的数据存储在 Log
table 上,考虑到我无法使用更改数据捕获,因为我使用的是 SQL Server Express 版本?
其他信息: Logs
table 中只有两列需要我帮助填写;来自合并的所有列的更改数据,示例如下:
CREATE TABLE USER_DATA
(
ID INT IDENTITY(1,1) NOT NULL,
NAME NVARCHAR2(25) NOT NULL,
PROFILE INT NOT NULL,
DATE_ADDED DATETIME2 NOT NULL
)
GO
CREATE TABLE AUDIT_LOG
(
ID INT IDENTITY(1,1) NOT NULL,
USER_ALTZ NVARCHAR(30) NOT NULL,
MACHINE SYSNAME NOT NULL,
DATE_ALTERERED DATETIME2 NOT NULL,
DATA_INSERTED XML,
DATA_DELETED XML
)
GO
我需要帮助填写的栏目是最后两栏(DATA_INSERTED
和 DATA_DELETED
)。我什至不确定数据类型是否应该是 XML,但是当有人
INSERTS
或 UPDATES
(仅限新值),USER_DATA
的 所有列 上的所有数据 inserted/updated 应该以某种方式合并到 DATA_INSERTED
.
DELETES
或 UPDATES
(仅限旧值),USER_DATA
的 所有列 上的所有数据 deleted/updated 应该以某种方式合并到 DATA_DELETED
.
可能吗?
Use the inserted and deleted Tables
DML trigger statements use two special tables: the deleted table and the inserted tables. SQL Server automatically creates and manages these tables. You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for DML trigger actions. You cannot directly modify the data in the tables or perform data definition language (DDL) operations on the tables, such as CREATE INDEX. In DML triggers, the inserted and deleted tables are primarily used to perform the following: Extend referential integrity between tables. Insert or update data in base tables underlying a view. Test for errors and take action based on the error. Find the difference between the state of a table before and after a data modification and take actions based on that difference.
和
Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. These results can be returned to the processing application for use in such things as confirmation messages, archiving, and other such application requirements. The results can also be inserted into a table or table variable. Additionally, you can capture the results of an OUTPUT clause in a nested INSERT, UPDATE, DELETE, or MERGE statement, and insert those results into a target table or view.
只是 posting,因为这解决了我的问题。正如用户@SeanLange 在对我的 post 的评论中所说,他让我使用 "audit"
,我不知道它存在。
谷歌搜索,引导我到 this Whosebug answer 第一个 link 有一个创建触发器的过程和 "shadow" 表做我需要的事情(它没有合并所有值都放在一列中,但它适合工作)。