how to get Last updated Record in sql server - sql

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()

Related

Gather all column names into a table

I am working on a project whos purpose is to rename all table names and column names in the sql database from one language to another. I have gather all the local table names into a table ltbl_TableNames and want to add all the columns of these tables into a table called ltbl_TableColumns.
I also want every table column to have a link to their table name. For example, the table 'Sales' have a column named 'Sum'. The table 'Sales' has the ID '10000'. I want to add that ID in a column named 'TableName_ID' for linking purposes. Is this possible to do without a lot of hassle?
Disclaimer: I am thinking about the renaming process. I only want to gather the column names with link to their parent table name.
Thanks in advance for answers.
You may use sys.tables for getting list of table name in your database.
Similarly for column name you may use information_schema.columns as this will give records with table name.
From the above 2 records you can easily make you required result.
;WITH CTABLE AS
( SELECT * FROM SYS.TABLES WHERE TYPE_DESC = 'USER_TABLE' )
, COLUMNNAME AS
( SELECT * FROM INFORMATION_SCHEMA.COLUMNS )
SELECT * INTO NEWTABLE FROM
(SELECT CTABLE.NAME AS TABLENAME , COLUMNNAME.COLUMN_NAME, COLUMNNAME.COLUMN_NAME + '_' + CAST(CTABLE.OBJECT_ID AS VARCHAR(15)) AS NEW_COLUMNNAME
FROM CTABLE INNER JOIN COLUMNNAME ON CTABLE.NAME = COLUMNNAME.TABLE_NAME ) AS D
You may try this for your result.

List all table names without knowing the database name within my machine

I am using SQl Server 2014, and Normally we can list out tables if we knew the DB name as :
USE YOURDBNAME
GO
SELECT *
FROM sys.Tables
GO
But I want to know all the tables irrespective of db's that are present on my machine
Or can I go for looping by listing out all DB name.(EXEC sp_databases)
Is there any better way to find out this?
There are several ways of getting all tables in your database
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'
OR
SELECT * FROM Sys.Tables
OR
SELECT * FROM INFORMATION_SCHEMA.TABLES
OR
SELECT sobjects.name FROM sysobjects sobjects WHERE sobjects.xtype = 'U'
Docs for all the xtypes
select * from tab
or
select * from * cat
The first one will show the associated CLUSTERID with tables for the current user.
I think you need
SELECT name FROM master.sys.databases
and then you will need to create a cursor query (for your USE XXXX) to iterate through the following query.
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE'

How to find last modification in database

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

What is the difference between last_user_update and modify_date?

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.

Why can't I query individual columns in a SQL view?

I've used a script to create a view that has the table name for every table in a database, along with the number of rows of data in the respective table. Everything works fine with SELECT *. However, I'd like to query only certain rows, but I can't seem to do that.
The view was created with the following script (credit to DatabaseZone.com for the script):
CREATE VIEW RowCount_AllTables
AS
SELECT DISTINCT
sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
FROM
sys.tables
INNER JOIN
sys.dm_db_partition_stats ON sys.tables.object_id = sys.dm_db_partition_stats.object_id
INNER JOIN
sys.schemas ON sys.tables.schema_id = sys.schemas.schema_id
WHERE
(sys.tables.is_ms_shipped = 0)
When I run Select * against the resulting view, I get results such as:
name tableName RowCount
dbo Table1 2
dbo Table2 1230
dbo Table3 0
If I query just the tableName column, that works fine and returns the table names. However, if I try to query the RowCount column as well, I get the error message Incorrect syntax near the keyword 'RowCount. This happens regardless of whether I qualify the database -- it seems to not recognize RowCount as a valid column that I can call in a query. So this script fails:
SELECT RowCount
FROM RowCount_AllTables;
But this one works:
SELECT tableName
FROM RowCount_AllTables;
What's going on? I'd like to be able to alias the column names in the view in my query but I can't do that so long as I can't reference the RowCount column.
(FYI running this in SQL Server 2014)
Rowcount is a reserved word, you can select reserved words using [] as:
[Rowcount]
Thanks to #sgeddes for pointing the way. Dropped the view, changed the script for creating it to use another name for the row count column, and it now works as expected. The issue was a conflict with Rowcount being a reserved word.
Changed the create table script on this line:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'RowCount'
to:
SELECT distinct sys.schemas.name, sys.tables.name AS tableName,
sys.dm_db_partition_stats.row_count AS 'Row_Count'
...at which point I can now reference the Row_Count column as desired.