goalie7960 goalie7960 - 3 months ago 9
SQL Question

SQL Server insert multiple records with same datetime

I have a table that has a datetime column that I wish to insert multiple records into at the same time. It is necessary that the datetime is exactly the same so I can late compare which records were inserted together. Is it sufficient to do something like this:

INSERT INTO Table(Date, ID, etc...)
VALUES (GETDATE(), ...)


Or should I declare a variable earlier in the stored procedure to hold the current time and just use that as the value to insert.

I have tested and it seems like the first method works fine, I just want to be 100% certain.




Edit: I think the question is better stated as "How often does GETDATE() get executed in a stored procedure?"

Answer

GETDATE() is evaluated once per query. There's no need to declare a variable.