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?"
GETDATE() is evaluated once per query. There's no need to declare a variable.
Yes, declare a variable to hold the current time if you want to use that approach. You could also create a parent record and assign a single datetime to it, and then write the parent ID into the child records. Relying on a DateTime stamp will probably work OK, but it seems a little dirty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With