Search code examples
sqlsql-server-2012sql-viewsysdatetime

View in SQL server 2012 with datetime function


What happens if use SYSUTCDATETIME() in a view? Will the view work correctly? When will it execute the SYSUTCDATETIME function?

For instance I ve created a view from the following select

SELECT * FROM dbo.contracts
WHERE (contract_start < SYSUTCDATETIME()) AND (contract_end > SYSUTCDATETIME())

Solution

  • It (SYSUTCDATETIME()) will return the value (as any other function) at the time of execution of the view.

    Execute this query several time and see for yourself

    SELECT *, SYSUTCDATETIME() sysutcdatetime FROM contracts
    WHERE (contract_start < SYSUTCDATETIME()) AND (contract_end > SYSUTCDATETIME());
    

    SQLFiddle