I have an application which stores all data in SQL Server 2008. This application creates, modifies, updates tables. The name of the database is XLPR_2001 and it contains nearly 500 tables.
I want to find out changes if I made through application where it affect the entire database (XLPR_2001). If I find it so I can directly made those changes directly in the database, I complete my work very fast.
I search it on web but not help me in my case.
As your Question, you are looking for Table Change Effect :
SELECT name [TableName],
Create_date [CreateDate],
modify_date [LastUpdate]
FROM sys.all_objects
WHERE type = 'U'
ORDER BY modify_date DESC;
From above SQL Command which would give you all Table_Name which are last effected by some activities (i.e. insert, update or delete).
Result :
#sarslash and #Yogesh in my case your code show very old result. I found below code on somewhere and it work perfectly.
select
object_name(object_id) as OBJ_NAME, *
from
sys.dm_db_index_usage_stats
where
database_id = db_id(db_name())
Order by
dm_db_index_usage_stats.last_user_update desc
You can find this info at modify_date column of sys.objects table
SELECT name, modify_date from sys.objects where type ='U' order by modify_date desc
Related
I'm trying to figure out when a table has been amended. Using the 2 different sets of code below, I get 2 different answers:
Select *
from sys.tables
where name = 'AN_StockChecks_000_Specs_010_StockChecks_010_PlantStockCheckSettings'
Output:
modify_date
-----------------------
2015-12-07 15:40:58.557
Approach #2:
SELECT
OBJECT_NAME(OBJECT_ID) AS DatabaseName, last_user_update, *
FROM
sys.dm_db_index_usage_stats
WHERE
database_id = DB_ID( 'IESA_DWHS')
AND ID = OBJECT_ID('AN_StockChecks_000_Specs_010_StockChecks_010_PlantStockCheckSettings')
Output:
last_user_update
-----------------------
2015-12-10 09:25:43.290
The modify_date from sys.tables is the date when the table structure was last modified - when you last added or dropped a column, or changed a column's properties.
Or as SQL Server Books Online puts it:
modify_date
Date the object was last modified by using an ALTER statement. If the object is a table or a view, modify_date also changes when a clustered index on the table or view is created or altered.
It has nothing to do with data operations on that table.
I use SQL Server 2008 R2.
I have a table with 5 records. The table does not have any column that keeps track of LastUpdated or something like that.
Is it possible to find out when was the last date/time that someone added/changed a record? Which record was that?
Thanks.
In General Practice you should add column to do this ,But any ways you can see the last time the table was Modified
if you want to check the Structure Modiifcation
USE
SELECT * FROM SYS.Tables where Name Like '[TableName]'
Here Column Modify_Date will give you last time table was modified
if you want to check the Data Modiifcation
USE
SELECT OBJECT_NAME(OBJECT_ID) AS TableName, last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( '[YourDatabaseName]')
If you want to know the latest modification datetime on that table then use
Select modify_date from sys.tables where name = 'tablename'
If you want to know the particular record then there is no way you have to use :
Alter table tablename add modifieddate datetime
select t.name, user_seeks, user_scans, user_lookups, user_updates, last_user_seek, last_user_scan, last_user_lookup, last_user_update
from sys.dm_db_index_usage_stats i JOIN sys.tables t ON (t.object_id = i.object_id)
where database_id = db_id()
I want to fetch the tables and stored procedures added in last 5 or 6 months to a particular database.
I have tried using the below command but it is not giving correct data.
select * from sys.objects
where type = 'U' or type = 'P'
and modify_date between '2012-09-01' and '2013-01-29'
Please suggest which command can give me this list.
If you are looking for objects ADDED, then why are you querying on modify_date? Shouldn't you be looking at create_date?
select * from sys.objects
where (type = 'U' or type = 'P')
and create_date between '2012-09-01' and '2013-01-29'
Also, you have not put your OR in parentheses, which means that you will get incorrect results anyway.
Raj
I need to search SQL Server's metadata for all stored procedures that were changed between specific dates.
So far I have following query that looking for specific value in the SP.
SELECT DISTINCT sysobjects.name AS [Object Name]
FROM sysobjects,syscomments
WHERE sysobjects.id = syscomments.id
and syscomments.text like '%Stock%'
In the example above I am looking for all SP that have word Stock in it.
But how can I search for all SP's that were changed between specific dates?
USE AdventureWorks;
GO
SELECT name, create_date, modify_date
FROM sys.objects
WHERE type = 'P'
AND name = 'uspUpdateEmployeeHireInfo'
GO
source: http://blog.sqlauthority.com/2007/08/10/sql-server-2005-find-stored-procedure-create-date-and-modified-date/
I want to check if there are any records in a table for a certain entry. I used COUNT(*) to check the number of records and got it to work. However, when the number of records for an entry is very high, my page loads slowly.
I guess COUNT(*) is causing the problem, but how do I check if the records exist without using it? I only want to check whether any records exist for the entry and then execute some code. Please help me find an alternative solution for this.
Thanks for any help.
There are several ways that may work. You can use exists, which lets the database optimise the method to get the answer:
if exists(select * from ...)
You can use top 1 so that the database can stop after finding the first match:
if (select count(*) from (select top 1 * from ...)) > 0
use select top 1 and check is there is an row
You can try selecting the first entry for given condition.
SELECT id FROM table WHERE <condition> LIMIT 1
I'm not sure if this will be quicker but you can try.
Other possible solution. How do you use count? COUNT(*)? If yes, then try using COUNT(id). As I remember this should be faster.
I would recommend testing to see if at least 1 record exists in the table, that meets your criteria then continue accordingly. For example:
IF EXISTS
(
SELECT TOP 1 Table_Name --Or Your ColumnName
FROM INFORMATION_SCHEMA.Tables -- Or your TableName
)
BEGIN
PRINT 'At least one record exists in table'
END
I found this on codeproject. It's quite handy.
-- Author,,Md. Marufuzzaman
SELECT SYS_OBJ.NAME AS "TABLE NAME"
, SYS_INDX.ROWCNT AS "ROW COUNT"
FROM SYSOBJECTS SYS_OBJ, SYSINDEXES SYS_INDX
WHERE SYS_INDX.ID = SYS_OBJ.ID
AND INDID IN(0,1) --This specifies 'user' databases only
AND XTYPE = 'U' --This omits the diagrams table of the database
--You may find other system tables will need to be ommitted,
AND SYS_OBJ.NAME <> 'SYSDIAGRAMS'
ORDER BY SYS_INDX.rowcnt DESC --I found it more useful to display
--The following line adds up all the rowcount results and places
--the final result into a separate column [below the first resulting table]
COMPUTE SUM(SYS_INDX.ROWCNT)
GO
you should use
select count(1) from
If you are saying (*) it will expand all the column's and then count