当我插入一条记录时,dateCreated和dateModified会被设置为默认的日期/时间。那么当我更新/修改记录时,dateCreated和dateModified会保留原样吗?我该怎么办?
只有在INSERT操作中才会使用列默认值,而不是UPDATE操作。如果您没有提供该列或在INSERT中使用DEFAULT关键字,则INSERT命令将使用默认值。
INSERT INTO Customer (col1, col2)
VALUES (..,..)
INSERT INTO Customer (col1, col2,dateCreated)
VALUES (..,..,DEFAULT)
INSERT INTO Customer (col1, col2,dateCreated,dateModified)
VALUES (..,..,DEFAULT,DEFAULT)
INSERT INTO Customer (col1, col2,dateCreated,dateModified)
VALUES (..,..,'1/1/2010',DEFAULT)
INSERT INTO Customer (col1, col2,dateCreated,)
VALUES (..,..,'1/1/2010')
INSERT INTO Customer (col1, col2,dateCreated,dateModified)
VALUES (..,..,'1/1/2010','1/2/2010')
我喜欢在过程的顶部使用本地变量集:
```html
我喜欢在过程的顶部使用本地变量集:
```
DECLARE @RunDate datetime
SET @RunDate=GETDATE()
我会在程序中使用这个时间戳,这样所有的更改(甚至是多个表格的更改)都会有精确到毫秒的相同日期。我还喜欢让dateModified列允许为空,并且没有默认值,当它被插入时,它是创建而不是修改,只有在实际修改时才会设置dateModified。
然后使用:
UPDATE Customer
SET importantColumn=
,dateModified = @RunDate
WHERE ...
UPDATE CustomerPrice
SET importantColumn=
,dateModified = @RunDate
WHERE ...