简体   繁体   中英

How to find last modified rows or records in a Sql table?

I have an employee table with their general information. how to find the last modified records from this particular table and besides I cannot add any more columns in this database?

If there is a DateModified column, or something like that, and you want the 10 most recent rows (for example) you could use a query like:

SELECT Top 10 * FROM myTable ORDER BY DateModified DESC;

You don't specify the flavor of SQL, so that query might be somewhat different if you're in Oracle, SQL Server, or MS Access.

If you don't have a column like that, and you want to know the most recently added records, if the primary key is an autoincrementing number, you could do the same thing, but sort by PrimaryKey descending. If you truly want modified (ie, updated) records, and you don't have a DateModified column, you're out of luck. There's no way to know.

Use MAX Aggregate function :

SELECT MAX(Modifieddate)
FROM your_table
WHERE your_conditions.  

Use this Code :

SELECT * 
FROM Update_Table UT1
WHERE UT1.Modified_Date = (SELECT MAX(UT2.Modified_Date) 
FROM Update_Table UT2 
WHERE UT2.PK = UT1.PK
AND UT2.Last_User = user)

使用此代码获取上次更新时间。

select top 1 a.object_id, b.name, a.last_user_update from sys.dm_db_index_usage_stats as a, sys.objects as b where a.object_id=b.object_id and b.name = 'your_table_name' order by last_user_update desc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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