简体   繁体   English

如何列出上周删除或更新数据的表格

[英]How to list tables where data was inserted deleted or updated in last week

I have sql server 2008 databases, I would like to know which tables was updated last week ie tables which has new rows, updated existing rows or which rows are deleted. 我有sql server 2008数据库,我想知道上周更新了哪些表,即具有新行,更新现有行或删除哪些行的表。

Is there any way to do this for existing database. 有没有办法为现有数据库执行此操作。

Try this one - 试试这个 -

SELECT 
      [db_name] = d.name
    , [table_name] = SCHEMA_NAME(o.[schema_id]) + '.' + o.name
    , s.last_user_update
FROM sys.dm_db_index_usage_stats s
JOIN sys.databases d ON s.database_id = d.database_id
JOIN sys.objects o ON s.[object_id] = o.[object_id]
WHERE o.[type] = 'U'
    AND s.last_user_update IS NOT NULL
    AND s.last_user_update BETWEEN DATEADD(wk, -1, GETDATE()) AND GETDATE()

Try with Change Data Capture . 尝试使用Change Data Capture It's a good way to keep track of your change on the DB. 这是跟踪数据库更改的好方法。 You have to enable the feature on one or more DBs, then on one or more table (it's a Table feature, so you will do it for every table you need). 您必须在一个或多个数据库上启用该功能,然后在一个或多个表上启用该功能(这是一个表功能,因此您将为所需的每个表执行此功能)。


Enable CDC on database. 在数据库上启用CDC。

Let's assume we want to enable CDC for AdventureWorks database. 假设我们要为AdventureWorks数据库启用CDC。 We must run the following SP to be sure this feature will work: 我们必须运行以下SP以确保此功能可用:

USE AdventureWorks 
GO 
EXEC sys.sp_cdc_enable_db 
GO

As result, we'll find a new schema called cdc and several tables automatically added: 结果,我们将找到一个名为cdc的新模式,并自动添加了几个表:

  • cdc.captured_columns – This table returns result for list of captured column. cdc.captured_columns - 此表返回捕获列列表的结果。
  • cdc.change_tables – This table returns list of all the tables which are enabled for capture. cdc.change_tables - 此表返回已启用捕获的所有表的列表。
  • cdc.ddl_history – This table contains history of all the DDL changes since capture data enabled. cdc.ddl_history - 此表包含自启用捕获数据以来所有DDL更改的历史记录。
  • cdc.index_columns – This table contains indexes associated with change table. cdc.index_columns - 此表包含与更改表关联的索引。
  • cdc.lsn_time_mapping – This table maps LSN number and time. cdc.lsn_time_mapping - 此表映射LSN编号和时间。

Enable CDC on table. 在桌面上启用CDC。

After having enabled CDC on desired DB(s) it's time to check if there are tables with this feature on: 在所需的数据库上启用CDC之后,是时候检查是否存在具有此功能的表:

USE AdventureWorks 
GO 
SELECT [name], is_tracked_by_cdc  
FROM sys.tables 
GO

If not, we can enable the changes capture for HumanResources.Shift table with the following procedure: 如果没有,我们可以使用以下过程为HumanResources.Shift表启用更改捕获:

USE AdventureWorks 
GO 
EXEC sys.sp_cdc_enable_table 
@source_schema = N'HumanResources', 
@source_name   = N'Shift', 
@role_name     = NULL 
GO

Be sure you SQL Agent is up and running because it will create a job (cdc.AdventureWorks_capture probably) to catch the modifications. 确保SQL Agent已启动并运行,因为它将创建一个作业(可能是cdc.AdventureWorks_capture)以捕获修改。 If all procedures are correctly executed we'll find a new table called cdc.HumanResources_Shift_CT, among the system tables, containing all the HumanResources.Shift changes. 如果所有过程都正确执行,我们将在系统表中找到一个名为cdc.HumanResources_Shift_CT的新表,其中包含所有HumanResources.Shift更改。

Note : be careful with @role_name parameter, it specifies database infos access. 注意 :注意@role_name参数,它指定数据库信息访问。

There is no way to find out this info by default unless you already have some auditing system installed. 除非您已经安装了一些审核系统,否则默认情况下无法找到此信息。

Only option, assuming your database is in full recovery mode, is to read transaction log and try get info from there. 假设您的数据库处于完全恢复模式,只有选项是读取事务日志并尝试从那里获取信息。

You can try reading transaction log using sql server functions DBCC LOG and fn_dblog or using third party tools such as ApexSQL Log . 您可以尝试使用sql server函数DBCC LOG和fn_dblog或使用第三方工具(如ApexSQL Log)读取事务日志。

As commented by others, Change Data Capture and Change Tracking would be the ideal solutions if you have implemented them. 正如其他人评论的那样,如果您实施了变更数据捕获和变更跟踪,它们将是理想的解决方案。 If you have not, then here is a quick way, which will give you info partially: 如果你还没有,那么这里有一个快速的方法,它将部分地给你信息:

SELECT OBJECT_NAME(OBJECT_ID) AS Object, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( <YourDBName>)
AND OBJECT_ID=OBJECT_ID(<YourTableName>)

This will tell you what tables were updated when, but may not really serve your purpose, if you are faced with a whodunnit scenario. 这将告诉您哪些表何时更新,但如果您遇到whodunnit场景,可能无法真正满足您的目的。 Also, it will only give you the latest updated timestamp, meaning, if someone updated last week and some one else updated yesterday, you will not see the last week update details. 此外,它只会为您提供最新更新的时间戳,这意味着,如果有人在上周更新并且其他人昨天更新了,您将看不到上周更新的详细信息。

Raj 拉吉

Try this.It will provide last updated date for each table in database. 试试这个。它将为数据库中的每个表提供最后更新的日期。

USE database_name

GO


SELECT OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update,*
FROM sys.dm_db_index_usage_stats

GO

For more reference click here 如需更多参考,请点击此处

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

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