日期:2014-05-19  浏览次数:20428 次

求一SQL语句。
求一   SQL   的   update的语句。
比如修改某字段(field)   改为一种递增的字符,由用户提供起点和终点。
修改结果:
004
005
....
010
011
....
059


不知道   update   语句能否实现,最好不要用多条语句来实现。

------解决方案--------------------
--环境和参数
declare @t table (id int,a varchar(10))

insert @t select 1, ' '
union all select 2, ' '
union all select 3, ' '
union all select 4, ' '
union all select 5, ' '
union all select 6, ' '
union all select 7, ' '

declare @Beg varchar(10)
set @Beg= '004 '

--开始处理
declare @a varchar(10)
set @a=right( '000 '+rtrim(cast(@beg as int)-1),3)
update @t
set @a=right( '000 '+rtrim(cast(@a as int)+1),3),a=@a

--显示结果
select * from @t

--结果
id a
----------- ----------
1 004
2 005
3 006
4 007
5 008
6 009
7 010

(所影响的行数为 7 行)