declare my_cursor cursor
  for select id from testtable
open my_cursor
fetch next from my_cursor
/*帮我实现下面这个update*/
/* update testtable set id=(case when (substring(id,1,1)='B') then '1'
  else '2' end)+(substring(id,2,1))+(substring(id,4,3)) 
update testtable set id='6'+id  */close my_cursor
deallocate my_cursorB10011   结果  611011
B20012        612012
C30013        623013
C40014        624014

解决方案 »

  1.   

    create table testtable(ID nvarchar(10))
    insert testtable select 'B10011'
    insert testtable select 'B20012'
    insert testtable select 'C30013'
    insert testtable select 'C40014'go
    --这样的情况不用游标,用语句效率高:
    update testtable
    set ID='6'+case when substring(ID,1,1)='B' then '1' else '2' end+substring(ID,2,1)+substring(ID,4,3)goID         
    ---------- 
    611011
    612012
    623013
    624014(所影响的行数为 4 行)
      

  2.   

    declare test  cursor for
    select ID from testtable order by ID 
    declare @ID nvarchar(10),@ID2 nvarchar(10)
    open test
    fetch next from test into @ID
    while @@fetch_status=0
    begin
    select @ID2='6'+case when substring(@ID,1,1)='B' then '1' else '2' end+substring(@ID,2,1)+substring(@ID,4,3) update testtable
    set ID=@ID2
    where ID=@ID fetch next from test into @ID
    end
    close test
    deallocate test
    goselect * from testtable
      

  3.   

    谢谢,,数据表有TRIGGER,数据量又大..所以用游标来完成.