如何写SQL语句设置子段的Default默认值?(20分)

  • 主题发起人 主题发起人 xingxingz
  • 开始时间 开始时间
X

xingxingz

Unregistered / Unconfirmed
GUEST, unregistred user!
如何写SQL语句设置子段的Default默认值?

tTemp
(
cvcName varchar(30) null
)
子段cvcName没有default值,怎么写个SQL语句设个default值,
cvcName varchar(30) null default 'B'

 
alter table tTemp
alter column
cvcName varchar(30) null default 'B'
 
下面是ms sql server2000中ALTER COLUMN的说明
ALTER COLUMN

Specifies that the given column is to be changed or altered. ALTER COLUMN is not allowed if the compatibility level is 65 or earlier. For more information, see sp_dbcmptlevel.

The altered column cannot be:

A column with a text, image, ntext, or timestamp data type.


The ROWGUIDCOL for the table.


A computed column or used in a computed column.


A replicated column.


Used in an index, unless the column is a varchar, nvarchar, or varbinary data type, the data type is not changed, and the new size is equal to or larger than the old size.


Used in statistics generated by the CREATE STATISTICS statement. First remove the statistics using the DROP STATISTICS statement. Statistics automatically generated by the query optimizer are automatically dropped by ALTER COLUMN.


Used in a PRIMARY KEY or [FOREIGN KEY] REFERENCES constraint.


Used in a CHECK or UNIQUE constraint, except that altering the length of a variable-length column used in a CHECK or UNIQUE constraint is allowed.


Associated with a default, except that changing the length, precision, or scale of a column is allowed if the data type is not changed.

以上说的是ALTER COLUMN不能达到的功能,看到最后一条,你就知道你的想法是不能实现的
 
取自联机帮助(ALTER TABLE)

F. 添加具有默认值的可为空的列
下例添加可为空的、具有 DEFAULT 定义的列,并使用 WITH VALUES 为表中的各现有行提供值。如果没有使用 WITH VALUES,那么每一行的新列中都将具有 NULL 值。

ALTER TABLE MyTable
ADD AddDate smalldatetime NULL
CONSTRAINT AddDateDflt
DEFAULT getdate() WITH VALUES

 

Similar threads

回复
0
查看
804
不得闲
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部