Does anyone know of a function as such that I can use to add an automatic date and timestamp in a column for when a user adds a record to the database table?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
This question already has answers here:
Closed 5 years ago.
回答1:
You can create a non-nullable DATETIME column on your table, and create a DEFAULT constraint on it to auto populate when a row is added.
e.g.
CREATE TABLE Example
(
SomeField INTEGER,
DateCreated DATETIME NOT NULL DEFAULT(GETDATE())
)
回答2:
You can use a datetime
field and set it's default value to GetDate()
.
CREATE TABLE [dbo].[Test](
[TimeStamp] [datetime] NOT NULL CONSTRAINT [DF_Test_TimeStamp] DEFAULT (GetDate()),
[Foo] [varchar](50) NOT NULL
) ON [PRIMARY]
回答3:
You can make a default constraint on this column that will put a default getdate() as a value.
Example:
alter table dbo.TABLE
add constraint df_TABLE_DATE default getdate() for DATE_COLUMN
回答4:
You can pass GetDate()
function as an parameter to your insert query
e.g
Insert into table (col1,CreatedOn) values (value1,Getdate())
回答5:
you can use DateAdd on a trigger or a computed column if the timestamp you are adding is fixed or dependent of another column