用语句添加列 ALTER TABLE #temp add @i numeric(18,2) null 请问怎样在添加列时的列名可以用变量,因为我需要添加用户输入的数量,并每个列名按日期的天数 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 declare @s varchar(100)set @s='ALTER TABLE #temp add '+rtrim(@i)+' numeric(18,2) null'exec @s 不支持, 可以用动态语句解决EXEC(N'ALTER TABLE #temp add ' + @i + ' numeric(18,2) null') declare @sql nvarchar(200)set @sql='ALTER TABLE #temp ADD ['+rtrim(@i)+'] numeric(18,2) null'exec(@sql) create table tb(col int)insert tbselect 1select * from tb/*col ----------- 1*/alter table tb add col2 int default 2 --默认值insert tb(col)select 2select * from tb/*col col2 ----------- ----------- 1 NULL2 2*/drop table tb 值是计算出来的,是有两个列的值相加的,可以写成 default a+b 这样的吗 create table tb(col int)insert tbselect 1select * from tb/*col ----------- 1*/alter table tb add col2 as col*2 --默认值insert tb(col)select 2select * from tb/*col col2 ----------- ----------- 1 32 4*/drop table tb sqlserver2000中系统临时表和用户临时表有什么区别? 求助三表连接join的用法?请大虾修正我的错误! 联合主键设置之后,设置外键的问题 SQL两张表一对多更新时,只更新一条记录 关于复合排序的sql语句问题 请教一下大家sql语句问题 sql server 数据库还原问题 一列中不同行的内容变成列. 紧急求助,紧急求助,表被锁住,很着急,效率问题, 求指点:怎样实现根据传入的公式计算结果 数据库索 创建,更新,重建的问题 无效资料行名称
set @s='ALTER TABLE #temp add '+rtrim(@i)+' numeric(18,2) null'
exec @s
EXEC(N'ALTER TABLE #temp add ' + @i + ' numeric(18,2) null')
set @sql='ALTER TABLE #temp ADD ['+rtrim(@i)+'] numeric(18,2) null'
exec(@sql)
insert tb
select 1select * from tb
/*
col
-----------
1
*/
alter table tb add col2 int default 2 --默认值
insert tb(col)
select 2select * from tb
/*
col col2
----------- -----------
1 NULL
2 2
*/
drop table tb
insert tb
select 1select * from tb
/*
col
-----------
1
*/
alter table tb add col2 as col*2 --默认值
insert tb(col)
select 2select * from tb
/*
col col2
----------- -----------
1 3
2 4
*/
drop table tb