日期:2014-05-18  浏览次数:20410 次

急求一个sql脚本
我要修改一个已有的表结构,
table1:
 id 名字 地址 电话
现在想在“地址”前加个字段“年龄” 类型为int ,not null
请问用sql 语句怎么实现

------解决方案--------------------
语句不能控制位置

------解决方案--------------------
语句无法控制字段位置,一个替代方案,
SQL code

--转存为table2
select id, 名字, 
0 '年龄', 地址, 电话
into table2
from table1

--删除table1
drop table table1

--重命名为table1
sp_rename 'table2','table1'

------解决方案--------------------
SQL code

alter table tabel1 add [年龄] int not null

------解决方案--------------------
SQL code

 if OBJECT_ID('tb') is not null 
 drop table tb 
 go 
 create table tb (id int,名字 varchar(50),地址 varchar(50),电话 varchar(50))
 
 select * from tb
 --现在想在“地址”前加个字段“年龄” 类型为int ,not null
alter table tb add   年龄 int NOT NULL
 select * from tb

id          名字                                                 地址                                                 电话
----------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------

(0 行受影响)

id          名字                                                 地址                                                 电话                                                 年龄
----------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------

(0 行受影响