Search code examples
sql-servermssql-jdbcgetdate

MSSQL fixed getdate() per query


If I need to update a few million rows with the current date, and I update them with the value getdate(), is this the query run time or the specific row runtime? Meaning will all rows have the same date or will some rows have values that are a few milliseconds off?

If it is the specific row time is there a way to insert the query start time instead?


Solution

  • If you have multiple statements in your batch you will get different values from getdate(). If you have only a single update then you will get the same value.

    If you have multiple statements you can declare a datetime variable and assign it the value of getdate(), then use that variable in your following statements.

    DECLARE @static_date DATETIME = getdate();
    
    UPDATE mytable SET MyDate = @static_date;
    UPDATE mytable2 SET YourDate = @static_date;
    ...