I have a SELECT statement that uses GETDATE() for two different column values. I'm wondering if by the nature of things those two separate function calls in the same SELECT will return identical values every time?
相关问题
- keeping one connection to DB or opening closing pe
- Quickest method for matching nested XML data again
- What are IN and OUT parameter in SQL Server
- How to migrate a database from SQL Server 2005 to
- Cannot insert NULL values into column 'USERNAM
相关文章
- Convert column to string in SQL Select
- Why not “Invalid column name XYZ” error in subquer
- Create @TableVariable based on an existing databas
- Convert bit type to Yes or No by query Sql Server
- Best Isolation Level to avoid deadlocks using an U
- How can i use pivot?
- Update and insert to one table from another
- Storing SSN/CC data
No they aren't guaranteed to return identical values every time. Each individual reference to
GetDate()
is a runtime constant and will keep its value throughout the query...will return the same value in all rows regardless of how long the query takes to run.
But there is no guarantee that different references will have the same value.
You can see this as below
Example Results
Yes, they will return the same date and time, to the millisecond.
Returns
I have tested this with 4000,
GETDATE()
and they all return the same.Sorry to say it, but I just came up with a test to show that it will not always return the same value. It actually does get evaluated twice, and if the system clock happens to turn over during the time between those two evaluations, you could get slightly different times out of the two calls.
However, the others are right in saying that it won't be evaluated once per row: only once per column.
See Will GETUTCDATE() return the same value if used twice in the same statement?
Most system functions are evaluated per query, not per row, except those that are by definition statistically unique such as NEWID(). This includes things like RAND() (which is not unique, only psuedo random) unless seeded randomly row by row with NEWD().
Determinism is not related to this evaluation because it means "the output is the same for any given input independent of, say, language or DMY/MDY)
This "once per query" evaluation makes sense of course, especially for GETDATE().
If I do a 10k row update, I want every row to have the same GETDATE() value. The entire update could easily take > 3.33 milliseconds and I don't want different values over my 10k rows.