就比如把表tab里符合条件的54行A字段换成另外54个数字。象下面的JR040005571,JR040005572﹐JR040005573...依次换成AAAA001,AAAA002,AAAA003.....怎幺用update语句直接实现?谢谢﹗
    A
JR040005571
JR040005572
JR040005573
JR040005574
JR040005575
JR040005576

解决方案 »

  1.   

    參考我這個帖子,可能對你有幫助
    http://expert.csdn.net/Expert/topic/2467/2467726.xml?temp=.1658136
      

  2.   

    也就是本来要一个一个来实现是这样的﹕
    Update tab set A=”AAAA001” where A=” JR040005571”
    Update tab set A=”AAAA002” where A=” JR040005572”
    .
    .
    .
    Update tab set A=”AAAA054” where A=” JR040005625”
    但现在由于有54条﹐或者更多﹐我想用一条语句或方法来一次性实现﹐而不是去写54条上面的update语句。
      

  3.   

    declare @a int
    set @a=0
    update tab set @a=@a+1,a='AAAA'+right(1000+@a,3) where 条件
      

  4.   

    declare @a int
    set @a=5571
    update tab 
       set 
         a='JR04000'+right(10000+@a,4) 
         ,@a=@a+1
    where 条件
      

  5.   

    用游标实现:declare cur cursor for select a  from tab
    declare @str char(11)
    declare @i int
    open cur
    declare @i2 int
    set @i2=571
    fetch next  from cur into @str
    set @i=1
    while @@fetch_status=0
    begin
    set @str=('AAAA0'+right('0'+cast(@i as varchar(2)),2))
    update tab set a=@str where a='JR040005'+cast(@i2 as varchar(3))
    set @i=@i+1
    set @i2=@i2+1
    fetch next  from cur into @str
    end
    close cur
    deallocate cur
    如果错误请告诉我,可以吗?