34,590
社区成员
发帖
与我相关
我的任务
分享
alter table student_info
add 学生姓名 varchar(10) not null
update student_info
set 学生姓名=姓名
alter table stedent_info
drop column 姓名
create table student_info(id int , 姓名 varchar(10))
go
alter table student_info add 学生姓名 varchar(10) null --如果是添加列,不能为 not null
go
update student_info set 学生姓名 = 姓名
alter table student_info drop column 姓名
go
drop table student_info
GO
IF OBJECT_ID('TBL')IS NOT NULL
DROP TABLE TBL
GO
CREATE TABLE TBL(
RName varchar(20),
SumQty decimal(18,2),
YearMonth varchar(6)
)
GO
INSERT TBL
SELECT '沪北小区',25452.00,'200901' UNION ALL
SELECT '沪北小区',35452.00,'200901' UNION ALL
SELECT '嘉定校区',26452.00,'200901' UNION ALL
SELECT '南校区', 25452.00,'200901' UNION ALL
SELECT '四平校区',25422.00,'200901' UNION ALL
SELECT '彭五小区',52452.00,'200901' UNION ALL
SELECT '沪北小区',21252.00,'200902' union all
SELECT '沪西小区',20452.00,'200902'
alter table TBL add RegionName varchar(20)
select * from tbl
update tbl set RegionName=RName
/*
RName SumQty YearMonth RegionName
沪北小区 25452.00 200901 沪北小区
沪北小区 35452.00 200901 沪北小区
嘉定校区 26452.00 200901 嘉定校区
南校区 25452.00 200901 南校区
四平校区 25422.00 200901 四平校区
彭五小区 52452.00 200901 彭五小区
沪北小区 21252.00 200902 沪北小区
沪西小区 20452.00 200902 沪西小区
*/
alter table student_info
add 学生姓名 varchar(10) not null
GO
update student_info
set 学生姓名=姓名
GO
alter table stedent_info
drop column 姓名
这样写就OK了