use current date as default value for a column

后端 未结 8 731
心在旅途
心在旅途 2021-02-02 04:43

Is there a way to set the default value of a column to DateTime.Now in Sql Server?

Example:

table Event
Id int (auto-increment) not null
Des         


        
相关标签:
8条回答
  • 2021-02-02 05:39

    Select Table Column Name where you want to get default value of Current date

     ALTER TABLE 
     [dbo].[Table_Name]
     ADD  CONSTRAINT [Constraint_Name] 
     DEFAULT (getdate()) FOR [Column_Name]
    

    Alter Table Query

    Alter TABLE [dbo].[Table_Name](
        [PDate] [datetime] Default GetDate())
    
    0 讨论(0)
  • 2021-02-02 05:44
    CREATE TABLE Orders(
        O_Id int NOT NULL,
        OrderNo int NOT NULL,
        P_Id int,
        OrderDate date DEFAULT GETDATE() // you can set default constraints while creating the table
    )
    
    0 讨论(0)
提交回复
热议问题