MSSQL修改表字段

修改表(将某列长度增加),此时原有的列不会被真正替换,而是是将新列加到该表最后,我们可以使用DBCC PAGE查看原有的数据仍然在那里。

示例:

use tempdb
go
create table a(col1 smallint,
			   col2 char(2000),
			   col3 char(1000)
			  )

此时在表a上修改列col2为char(3000)

alter table a 
alter column col2 char(3000)

在我们理解中,该表目前单行数据长度4002再加内部使用的一点长度。此时即使再加一个3000长度的列,也不会报错。

alter table a 
add col4 char(3000)

但实际是:

Msg 1701, Level 16, State 1, Line 1
Creating or altering table 'a' failed because the minimum row size would be 9009, including 7 bytes of internal overhead. This exceeds the maximum allowable table row size of 8060 bytes.

此时,可以有以下方法来修复:

1.

 alter table a rebuild

2.

create clustered index ix_a 
on a(col1)

3.

使用最新脚本创建a_bak,将数据全部导入到a_bak之后,删除a表,再将a_bak表重命名为a。

 

posted @ 2015-05-02 11:05  Wison-Ho  阅读(546)  评论(0编辑  收藏  举报