There are several incorrect code segments. I have fixed the SQLEagle code snippet above, and now you really should see the last changed dates - I believe that the changed date should come from sys.objects, and not from sys.tables. I see that Andrew Arnold commented on the Debbie code snippet โexactly the sameโ. However, Andrew clearly did not execute these two code segments, or he would have known that 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 according to 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
source share