当前位置: 代码迷 >> Sql Server >> 如何样在脚本中为字段指定默认值
  详细解决方案

如何样在脚本中为字段指定默认值

热度:52   发布时间:2016-04-27 21:13:58.0
怎么样在脚本中为字段指定默认值?
我的表结构都是写好脚本后创建的。
可是我怎么样在脚本中为字段指定默认值呢?
用企业管理器我会做,可是我需要在脚本中实现
比如:有一个stsdate的字段,我要指定其默认值为(getdate()),应该如何
修改下面的脚本呀?

create   table   [dbo].[t_employee_wage_history](
[id]   [int]   identity   not   null,
[employee_id]   [int]   not   null,
[ymd]   [int]   not   null,
[wage_change_type]   [int]   not   null,
[wage_item_id]   [int]   not   null,
[wage]   [decimal](19,   2)   not   null,
[stsdate]   [datetime]   not   null
)   on   [primary]
go


------解决方案--------------------
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null Default GetDate()
) on [primary]
go
------解决方案--------------------
CREATE TABLE [dbo].[tb] (
[id] [char] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[dt] AS (getdate())
) ON [PRIMARY]
GO
------解决方案--------------------
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null AS (getdate())
) on [primary]
go

------解决方案--------------------
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null default getdate()
) on [primary]
go
------解决方案--------------------
--先建立表
create table [dbo].[t_employee_wage_history](
[id] [int] identity not null,
[employee_id] [int] not null,
[ymd] [int] not null,
[wage_change_type] [int] not null,
[wage_item_id] [int] not null,
[wage] [decimal](19, 2) not null,
[stsdate] [datetime] not null
) on [primary]
go
--后增加一个默认值的约束
alter table [t_employee_wage_history] add constraint df_wanghistory default getdate()for [stsdate]
  相关解决方案