SQL Server – How to Query Which Table Is Inserted Mostly

sql serversql server 2014sql-server-2012sql-server-2016

I have a database and there is not any trigger which follows the transactions in said database.

Now, I have to make a report that shows which table has mostly inserted so far. How can I do that?

Best Answer

To know mostly inserted table you can simply retrieve number of rows/records in the table.

You can use following query to find it. Mostly inserted tables will be displayed on top of the records.

SELECT
   o.Name TableName,SUM(ps.row_count) Total_Rows
FROM
            sys.dm_db_partition_stats ps
inner join  sys.objects o
on ps.object_id=o.object_id
WHERE o.type='U'
GROUP BY
    o.Name
ORDER BY 2 DESC

Edit:1

As you described you requirement in comments, "you need find mostly inserted tables between a date range. It can be done using several method.

  1. Using AFTER TRIGGER on the tables.
  2. Using IDENTITY type column along with a column which records insert date & time (like Amina commented).
  3. Using CDC you can record date & time of every changes in the table.
  4. enabling Temporal Table (SQL Server 2016 +) feature on a table to record every changes.
  5. Track Data Changes (SQL Server 2017)

Thanks!