实时 SQL Server 2005 数据库的上次修改日期

发布于 2024-09-28 07:32:09 字数 412 浏览 3 评论 0原文

有没有办法知道实时 SQL Server 2005 数据库中最后一次写入操作发生的时间?

目前我的服务器上有 30 个(并且还在不断增加)数据库。只有其中一些可以看到日常活动(但哪些获得日常活动会随着时间的推移而变化。)我的备份空间有限,我想对所有“自上次备份以来修改的”数据库进行每日备份。

事实上,我问的是 的相反内容这个问题。我不想询问备份的上次写入日期来查看是否应该恢复它,而是想告诉实时数据库的上次写入日期来查看是否应该备份它。

由于备份在服务器本身上运行,我可以检查日志的上次修改时间,但这不是很干净,我也不确定是否完全可靠。

Is there a way to know when the last write operation has occurred in a live SQL Server 2005 database?

I currently have 30 (and growing) databases on my server. Only some of these see daily activity (but which ones get daily activity varies over time.) My backup space is limited and I'd like to do a daily backup of all "modified since last backup" databases.

In fact, I'm asking the reverse of this question. Instead of asking last write date from a backup to see if I should restore it, I want to tell last write date of a live database to see if I should back it up.

Since the backups run on the server itself, I could check the last modification time of the log, but that isn't very clean, nor I'm sure is totally reliable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

笛声青案梦长安 2024-10-05 07:32:09

这会有所帮助吗:

SELECT max(last_user_update) last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'YOUR_DBNAME_HERE')

Would this help:

SELECT max(last_user_update) last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'YOUR_DBNAME_HERE')
梦晓ヶ微光ヅ倾城 2024-10-05 07:32:09

此博客条目包含以下信息:如何在 SQL Server 2008 和 2005 上执行此操作。

  • 在 2008 上:使用新的服务器审核功能
  • 在 2005 上:使用动态管理视图 (DMV)

This Blog entry contains information on how to do it on SQL Server 2008 and 2005.

  • On 2008: using the new Server Auditing feature
  • On 2005: using Dynamic Management Views (DMV)
风月客 2024-10-05 07:32:09

您可能需要重新考虑您的备份策略,也许可以将每周完整备份作为基准,然后运行 ​​本周差异备份。当然,这一切都取决于您与恢复业务的 SLA。

You might want to rethink your backup strategy, perhaps by taking a weekly full backup as a baseline and then running differential backups during the week. Of course, this all depends on your SLA with the business for recovery.

你的他你的她 2024-10-05 07:32:09

levidos 抱歉,您得到的信息不正确。因为您没有对每个数据库的前一个进行排序,所以您实际上没有获得最后修改日期,而是获得了第一个返回的记录。

请将我的结果与你的结果进行比较。

这是我的更新。

DECLARE @sqlString NVARCHAR(MAX) ,
    @union NVARCHAR(MAX) ,
    @name NVARCHAR(50),
    @Counter AS Int

SET @sqlString = ''
SET @union = ''
SET @counter = 0

DECLARE crs CURSOR
FOR
    SELECT  Name
    FROM    sys.databases
    WHERE   state = 0 
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0 
    BEGIN

        SET @counter = @counter + 1

        SET @sqlString = @sqlString + @union
        SET @sqlString = @sqlString + ' 
    SELECT  *
    FROM    ( 
        SELECT TOP 1
           ''' + @name + ''' as DBName, modify_date
        FROM [' + @name + '].sys.tables 
        ORDER BY modify_date DESC
             ) as Table' + CAST(@Counter AS VARCHAR(20))

        SET @union = '  UNION '

        FETCH NEXT FROM crs INTO @name

    END 

--PRINT @sqlString 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs ;
DEALLOCATE crs ;
EXEC(@sqlString)

levidos sorry but you get incorrect information. Because you are not sorting your top one per database so you really aren't getting the LAST MODIFIED DATE you're getting the first returned record.

Please compare my results to yours.

Here's my update.

DECLARE @sqlString NVARCHAR(MAX) ,
    @union NVARCHAR(MAX) ,
    @name NVARCHAR(50),
    @Counter AS Int

SET @sqlString = ''
SET @union = ''
SET @counter = 0

DECLARE crs CURSOR
FOR
    SELECT  Name
    FROM    sys.databases
    WHERE   state = 0 
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0 
    BEGIN

        SET @counter = @counter + 1

        SET @sqlString = @sqlString + @union
        SET @sqlString = @sqlString + ' 
    SELECT  *
    FROM    ( 
        SELECT TOP 1
           ''' + @name + ''' as DBName, modify_date
        FROM [' + @name + '].sys.tables 
        ORDER BY modify_date DESC
             ) as Table' + CAST(@Counter AS VARCHAR(20))

        SET @union = '  UNION '

        FETCH NEXT FROM crs INTO @name

    END 

--PRINT @sqlString 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs ;
DEALLOCATE crs ;
EXEC(@sqlString)
一身软味 2024-10-05 07:32:09

上面有一些不正确的代码段。我已经修复了上面的 SQLEagle 的代码片段,现在您确实应该看到最后修改日期 - 我相信修改日期应该来自 sys.objects,而不是 sys.tables。我看到安德鲁·阿诺德评论黛比的代码片段“完全相同”。然而,Andrew 显然没有运行这两个代码段,否则他会知道 Debbie 的结果比 WaterCooler 的贡献更好,实际上提供了几乎正确的结果,但可以按如下进一步改进。

DECLARE @sqlString NVARCHAR(MAX) ,
    @union NVARCHAR(MAX) ,
    @name NVARCHAR(50),
    @Counter AS Int

SET @sqlString = ''
SET @union = ''
SET @counter = 0

DECLARE crs CURSOR
FOR
    SELECT  Name
    FROM    sys.databases
    WHERE   state = 0 
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0 
    BEGIN

        SET @counter = @counter + 1

        SET @sqlString = @sqlString + @union
        SET @sqlString = @sqlString + ' 
    SELECT  *
    FROM    ( 
        SELECT TOP 1
           ''' + @name + ''' as DBName, modify_date
        FROM [' + @name + '].sys.objects 
        ORDER BY modify_date DESC
             ) as Table' + CAST(@Counter AS VARCHAR(20))

        SET @union = '  UNION '

        FETCH NEXT FROM crs INTO @name

    END 

--PRINT @sqlString 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs ;
DEALLOCATE crs ;
EXEC(@sqlString)

There are a number of incorrect code segments above. I have fixed SQLEagle's code snippet above and now you should indeed see the last modified dates - I believe modified date should come from sys.objects, not sys.tables. I see Andrew Arnold commented on Debbie's code snippet "being exactly the same". Andrew clearly didn't run these two code segments however or he would know Debbie's result gets a better result than WaterCooler's contribution in terms of actually providing an almost correct result, but could be further improved as per below.

DECLARE @sqlString NVARCHAR(MAX) ,
    @union NVARCHAR(MAX) ,
    @name NVARCHAR(50),
    @Counter AS Int

SET @sqlString = ''
SET @union = ''
SET @counter = 0

DECLARE crs CURSOR
FOR
    SELECT  Name
    FROM    sys.databases
    WHERE   state = 0 
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0 
    BEGIN

        SET @counter = @counter + 1

        SET @sqlString = @sqlString + @union
        SET @sqlString = @sqlString + ' 
    SELECT  *
    FROM    ( 
        SELECT TOP 1
           ''' + @name + ''' as DBName, modify_date
        FROM [' + @name + '].sys.objects 
        ORDER BY modify_date DESC
             ) as Table' + CAST(@Counter AS VARCHAR(20))

        SET @union = '  UNION '

        FETCH NEXT FROM crs INTO @name

    END 

--PRINT @sqlString 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs ;
DEALLOCATE crs ;
EXEC(@sqlString)
鸵鸟症 2024-10-05 07:32:09

这将列出服务器上的所有在线数据库以及最后修改日期

DECLARE @sqlString NVARCHAR(max)
DECLARE @union NVARCHAR(max)
SET @sqlString = ''
SET @union = ''
DECLARE @name nvarchar(50);

DECLARE crs CURSOR FOR 
SELECT Name FROM sys.databases WHERE  state = 0
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
   SET @sqlString = @sqlString + @union 
   SET @sqlString = @sqlString + '
    SELECT 
    TOP 1
  ''' + @name + ''' as DBName, modify_date
  FROM
   [' + @name + '].sys.tables'

 SET @union = ' UNION '

    FETCH NEXT FROM crs INTO @name
END 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs;
DEALLOCATE crs;
EXEC(@sqlString)

This one will list all online databases on the server and the last modified date

DECLARE @sqlString NVARCHAR(max)
DECLARE @union NVARCHAR(max)
SET @sqlString = ''
SET @union = ''
DECLARE @name nvarchar(50);

DECLARE crs CURSOR FOR 
SELECT Name FROM sys.databases WHERE  state = 0
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
   SET @sqlString = @sqlString + @union 
   SET @sqlString = @sqlString + '
    SELECT 
    TOP 1
  ''' + @name + ''' as DBName, modify_date
  FROM
   [' + @name + '].sys.tables'

 SET @union = ' UNION '

    FETCH NEXT FROM crs INTO @name
END 

SET @sqlString = @sqlString + ' ORDER BY DBName ASC'
CLOSE crs;
DEALLOCATE crs;
EXEC(@sqlString)
黒涩兲箜 2024-10-05 07:32:09
DECLARE @sqlString NVARCHAR(max) = ''
DECLARE @union NVARCHAR(max) = ''
DECLARE @name nvarchar(50) 

DECLARE crs CURSOR FOR 
SELECT Name FROM sys.databases WHERE  state = 0
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
   SET @sqlString = @sqlString + @union 
   SET @sqlString = @sqlString + '
    SELECT  
  ''' + @name + ''' as DBName, Max( modify_date) as modDate
  FROM
   [' + @name + '].sys.tables'

 SET @union = ' UNION '

    FETCH NEXT FROM crs INTO @name
END 

SET @sqlString = @sqlString + ' ORDER BY modDate desc'
CLOSE crs;
DEALLOCATE crs;
EXEC(@sqlString) 
DECLARE @sqlString NVARCHAR(max) = ''
DECLARE @union NVARCHAR(max) = ''
DECLARE @name nvarchar(50) 

DECLARE crs CURSOR FOR 
SELECT Name FROM sys.databases WHERE  state = 0
OPEN crs
FETCH NEXT FROM crs INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
   SET @sqlString = @sqlString + @union 
   SET @sqlString = @sqlString + '
    SELECT  
  ''' + @name + ''' as DBName, Max( modify_date) as modDate
  FROM
   [' + @name + '].sys.tables'

 SET @union = ' UNION '

    FETCH NEXT FROM crs INTO @name
END 

SET @sqlString = @sqlString + ' ORDER BY modDate desc'
CLOSE crs;
DEALLOCATE crs;
EXEC(@sqlString) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文