When we update the data in custom table current datetime and current user details can we capture in 2 different columns for custom table?

CS, Dileepkumar 1 Reputation point

Hello Team,

When I inserted data into the DB table I have created 2 columns to capture the current date time and similarly capturing current user details when I inserted data into the DB.
if data is already inserted in the DB table once we try to update data in the table those 2 columns will not capture the current date time and current user details .


SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,884 questions
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,563 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 41,101 Reputation points

    That's to less on informations.
    How does your implementation looks like? Column default value, setting the timestamp in UPDATE statement or a trigger?

  2. Erland Sommarskog 102.3K Reputation points

    The best way to do this, is write the update as:

    UPDATE tbl
    SET       coL = @somevalue,
           current_time = DEFAULT,
           current_user = DEFAULT
    FROM  tbl
    WHERE ...

    Yeah, I know that this is not what you are looking for. You want it to happen automatically, and this can be done with a trigger, but that comes with a performance overhead.

    It is a long-standing request that Microsoft provides something better, and your request is absolutely reasonable. You can vote for a improvement suggestion here: http://feedback.azure.com/forums/908035-sql-server/suggestions/32901964-provide-a-mechanism-for-columns-to-automatically-u

    0 comments No comments

  3. MelissaMa-MSFT 24,176 Reputation points

    Hi @CS, Dileepkumar ,

    Welcome to Microsoft Q&A!

    You could refer below example which created one trigger to update the update time and update user automatically.

    Step1: create one table as below.

    create table testtable  
    (id int ,  
    col1 varchar(10),  
    updatetime datetime,  
    updateduser varchar(10))  
    insert into testtable values  

    Step2: create one trigger as below.

    create trigger updatetrigger on testtable  
    after update  
    update a  
    set a.updatetime=GETDATE()  
    from  testtable a  
    inner join inserted b on a.id=b.id  

    Step3: update one row and compare the results.

    select * from testtable  
    update testtable set col1='b' where id=1  
    select * from testtable  


    Best regards,

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.