读书人

更新难题求救!解决思路

发布时间: 2012-01-22 22:38:43 作者: rapoo

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

例:

表 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
*/

读书人网 >SQL Server

热点推荐