日期:2014-05-19  浏览次数:20530 次

怎么样在脚本中为字段指定默认值?
我的表结构都是写好脚本后创建的。
可是我怎么样在脚本中为字段指定默认值呢?
用企业管理器我会做,可是我需要在脚本中实现
比如:有一个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]