When I insert a record, dateCreated and dateModified gets the default date / time. When I update / modify record, does dateModified and dateCreated remain as it is? What should I do?
The default value for a column is used only in INSERT ing, not UPDATE . The default value will be used by the INSERT command if you do not specify a column or run the DEFAULT keyword in INSERT.
INSERT INTO Customer (col1, col2) VALUES (..,..) ---get default for dateCreated & dateModified INSERT INTO Customer (col1, col2,dateCreated) VALUES (..,..,DEFAULT) ---get default for dateCreated & dateModified INSERT INTO Customer (col1, col2,dateCreated,dateModified) VALUES (..,..,DEFAULT,DEFAULT) ---get default for dateCreated & dateModified INSERT INTO Customer (col1, col2,dateCreated,dateModified) VALUES (..,..,'1/1/2010',DEFAULT) ---only get default for dateModified INSERT INTO Customer (col1, col2,dateCreated,) VALUES (..,..,'1/1/2010') ---only get default for dateModified INSERT INTO Customer (col1, col2,dateCreated,dateModified) VALUES (..,..,'1/1/2010','1/2/2010') ---no defaults for dateCreated & dateModifie
I like to use the local set of variables, which is located at the top of the procedure:
DECLARE @RunDate datetime SET @RunDate=GETDATE()
Then I use this in the procedure, so all changes (even on several tables) have exactly the same date for the millisecond. I also prefer the dateModified column to allow null and not have a default value when it is inserted, it was created not changed, I will set dateModified when it is actually changed.
then use:
UPDATE Customer SET importantColumn= ,dateModified = @RunDate WHERE ... UPDATE CustomerPrice SET importantColumn= ,dateModified = @RunDate WHERE ...