首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

更新难题求救!解决思路

2012-01-22 
更新难题求救!!我需要对一个表里符合条件的某个字段进行赋值更新,按一个整数值递增。请问有哪位兄弟知道怎

更新难题求救!!
我需要对一个表里符合条件的某个字段进行赋值更新,按一个整数值递增。请问有哪位兄弟知道怎么实现啊?小弟感激不尽!

例:

表   T   有字段A   ,A初始为NULL。另有整数   I=100
要实现将T表所有A字段为   NULL的记录查出,然后将I   的值写到   A,I   每赋值一条后自加   2



[解决办法]
declare @t table(A int)
insert @t
select 1 union all
select NULL union all
select NULL union all
select NULL union all
select 4

----更新
declare @i int
set @i = 100
update @t set A = @i,@i = @i + 2 where A is null

----查看
select * from @t

/*结果
1
102
104
106
4
*/

热点排行