简体   繁体   English

触发记录插入/更新/删除的值SQL Server 2012

[英]Trigger to log inserted/updated/deleted values SQL Server 2012

I'm using SQL Server 2012 Express and since I'm really used to PL/SQL it's a little hard to find some answers to my T-SQL questions. 我正在使用SQL Server 2012 Express,因为我已经习惯了PL / SQL,所以找到我的T-SQL问题的答案有点困难。

What I have: about 7 tables with distinct columns and an additional one for logging inserted/updated/deleted values from the other 7. 我所拥有的:约7个具有不同列的表和另一个用于记录其他7中的插入/更新/删除值的表。

Question: how can I create one trigger per table so that it stores the modified data on the Log table, considering I can't used Change Data Capture because I'm using the SQL Server Express edition? 问题:我如何为每个表创建一个触发器,以便将修改后的数据存储在Log表中,考虑到我不能使用Change Data Capture,因为我使用的是SQL Server Express版本?

Additional info: there is only two columns in the Logs table that I need help filling; 附加信息: Logs表中只有两列我需要帮助填写; the altered data from all the columns merged, example below: 合并的所有列中的更改数据,如下所示:

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

The columns I need help filling are the last two ( DATA_INSERTED and DATA_DELETED ). 我需要帮助填充的列是最后两列( DATA_INSERTEDDATA_DELETED )。 I'm not even sure if the data type should be XML, but when someone either 我甚至不确定数据类型是否应该是XML,但是当有人时

INSERTS or UPDATES (new values only), all data inserted/updated on the all columns of USER_DATA should be merged somehow on the DATA_INSERTED . INSERTSUPDATES (仅限新值),在USER_DATA所有列上插入/更新的所有数据应以某种方式合并在DATA_INSERTED

DELETES or UPDATES (old values only), all data deleted/updated on the all columns of USER_DATA should be merged somehow on the DATA_DELETED . DELETESUPDATES (仅限旧值), USER_DATA所有列上删除/更新的所有数据都应在DATA_DELETED上以某种方式合并。

Is it possible? 可能吗?

Use the inserted and deleted Tables 使用inserted和deleted表

DML trigger statements use two special tables: the deleted table and the inserted tables. DML触发器语句使用两个特殊表:已删除的表和插入的表。 SQL Server automatically creates and manages these tables. SQL Server自动创建和管理这些表。 You can use these temporary, memory-resident tables to test the effects of certain data modifications and to set conditions for DML trigger actions. 您可以使用这些临时的内存驻留表来测试某些数据修改的效果,并为DML触发器操作设置条件。 You cannot directly modify the data in the tables or perform data definition language (DDL) operations on the tables, such as CREATE INDEX. 您不能直接修改表中的数据或对表执行数据定义语言(DDL)操作,例如CREATE INDEX。 In DML triggers, the inserted and deleted tables are primarily used to perform the following: Extend referential integrity between tables. 在DML触发器中,inserted和deleted表主要用于执行以下操作:扩展表之间的引用完整性。 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. 找出数据修改之前和之后表的状态之间的差异,并根据该差异采取措施。

And

OUTPUT Clause (Transact-SQL) OUTPUT子句(Transact-SQL)

Returns information from, or expressions based on, each row affected by an INSERT, UPDATE, DELETE, or MERGE statement. 返回受INSERT,UPDATE,DELETE或MERGE语句影响的每一行的信息或表达式。 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. 此外,您可以在嵌套的INSERT,UPDATE,DELETE或MERGE语句中捕获OUTPUT子句的结果,并将这些结果插入目标表或视图中。

Just posting because this is what solved my problem. 只是张贴,因为这是解决我的问题。 As user @SeanLange said in the comments to my post, he said to me to use an "audit" , which I didn't know it existed. 正如用户@SeanLange在我的帖子的评论中所说,他告诉我使用"audit" ,我不知道它存在。

Googling it, led me to this Stackoverflow answer where the first link there is a procedure that creates triggers and "shadow" tables doing sort of what I needed (it didn't merge all values into one column, but it fits the job). 谷歌搜索它,引导我到这个Stackoverflow的答案 ,其中第一个链接是一个程序,创建触发器和“影子”表做我需要的一切(它没有将所有值合并到一个列,但它适合工作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM