求一sql
现表中已有以下数据 
 单据号    单内序号 
 ---------------------- 
 001                                 0 
 001                                 0 
 001                                 0 
 002                                 0 
 002                                 0   
 改为: 
 单据号    单内序号 
 ---------------------- 
 001                                 1 
 001                                 2 
 001                                 3 
 002                                 1 
 002                                 2
------解决方案--------------------用游标吧,简单 SQL 比较困难!
------解决方案--------------------直接在数据里该?用游标
------解决方案--------------------感觉这样不是很好 
 create table tableMain  
 ( 
 	单据号  varchar(50), 
 	单内序号 int 
 )   
 insert into tableMain values( '001 ',0) 
 insert into tableMain values( '001 ',0) 
 insert into tableMain values( '001 ',0) 
 insert into tableMain values( '002 ',0) 
 insert into tableMain values( '002 ',0)   
 create table tableTemp  
 ( 
 	单据号  varchar(50), 
 	单内序号 int IDENTITY (1, 1) NOT NULL 
 )   
 declare @orderID varchar(50)   
 declare mycursor cursor for  select distinct 单据号 from tableMain order by 单据号 
 open mycursor 
 fetch next from mycursor into @orderID 
 while (@@FETCH_STATUS=0) 
 begin 
 	insert into tableTemp select 单据号 from tableMain  where 单据号 = @orderID 
 	delete tableMain where 单据号 = @orderID 
 	insert into tableMain select * from tableTemp 
 	truncate table tableTemp 
 	fetch next from mycursor into @orderID 
 end 
 close mycursor 
 deallocate mycursor   
 select * from tableMain   
 drop table tableTemp 
 drop table tableMain
------解决方案----------------------創建測試環境 
 Create Table TEST 
 (单据号 Char(3), 
  单内序号 Int) 
 --插入數據 
 Insert TEST Select  '001 ',           0 
 Union All Select  '001 ',           0 
 Union All Select  '001 ',           0 
 Union All Select  '002 ',           0 
 Union All Select  '002 ',           0 
 GO 
 --測試 
 Declare @I Int, @单据号 Char(3) 
 Select @I = 1, @单据号 =  ' ' 
 Update  
 	TEST  
 Set  
 	@I = (Case When @单据号 = 单据号 Then @I + 1 Else 1 End), 
 	单内序号 = @I, 
 	@单据号 = 单据号   
 Select * From TEST 
 GO 
 --刪除測試環境 
 Drop Table TEST 
 --結果 
 /* 
 单据号	单内序号 
 001	1 
 001	2 
 001	3 
 002	1 
 002	2 
 */
------解决方案--------------------楼上的有各问题;如果单据号的顺序错乱的话就不行了
------解决方案--------------------declare tmp_orderId varchar2(3); 
 declare begin_id integer; 
 declare tmp_order varchar2(3); 
 declare tmp_insideId integer; 
 cursor tmp_cursor is select orderId insideId from 表名间.表名; 
 begin  
 tmp_orderId :=  ' '; 
 open tmp_cursor; 
 loop