Tuesday, 25 May 2021

How to find when column was added in table in sql server

Suppose we have a table and recently we have added a column on the table. For audit the purpose I want to know when the new columns are added.

See the example.

I have an EMP table. There are 4 columns in that table

     
    
This table is created on 14-March-2021.

SELECT o.name,o.type,o.type_desc,o.create_date from sys.tables o

where o.name='Emp'

  

Now I am adding one column in this table.

ALTER TABLE Emp

ADD Salary decimal(28,10)

   

Now I want to see when the new column is added.

With the help of below sql script we will get when the column is updated or created.

SELECT Object_name(sc.[object_id]) AS [table],
       
sc.[name]                   AS [column],
       
so.modify_date,
       
so.create_date
FROM   [sys].[columns] sc
       
JOIN [sys].[objects] so
         
ON sc.[object_id] = so.[object_id]
WHERE  Object_name(sc.[object_id]) = 'emp'
       
AND sc.NAME = 'Salary'
ORDER  BY so.modify_date DESC,
          
so.create_date ASC 


  

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts