drop table #temp
select identity(int,1,1) as id,left(col,charindex('=',col)-1) as firstcol,'('+right(col,len(col)-charindex('=',col))+')' as lastcol,right(col,len(col)-charindex('=',col)) as RuntimeValue,xh=1 into #temP from temp1216
/*
得到如表:
id    firstcol    lastcol     RuntimeValue    xh
1 b (d+e) d+e 1
2 a (b+c) b+c 1
3 d (f-g) f-g 1

*/
select * from #temp
declare @I int,@J int,@firstcol varchar(100),@lastcol varchar(100)
select @J=0
select @I=2
while @I<3
--@J<>(select max(len(runtimeValue)) from #temp)
begin
       print @i
select @j=max(len(runtimeValue)) from #temp
DECLARE Updatetemp CURSOR local FOR 
SELECT firstcol,lastcol
FROM #temp
OPEN Updatetemp FETCH NEXT FROM Updatetemp
INTO @firstcol,@lastcol
WHILE @@FETCH_STATUS = 0
Begin
update #temp set runtimeValue=replace(runtimevalue,@firstcol,@lastcol) FETCH NEXT FROM Updatetemp
INTO @firstcol,@lastcol
End CLOSE Updatetemp
DEALLOCATE Updatetemp select @I=@I+1
End
update #temp set xh=len(runtimevalue)-len(replace(runtimevalue,'(',''))+1select * from #temp
---------------
1 b (d+e) (f-g)+e 2
2 a (b+c) ((f-g)+e)+c 3
3 d (f-g) f-g 1

解决方案 »

  1.   

    drop table #temp
    select identity(int,1,1) as id,left(col,charindex('=',col)-1) as firstcol,'('+right(col,len(col)-charindex('=',col))+')' as lastcol,right(col,len(col)-charindex('=',col)) as RuntimeValue,xh=1 into #temP from temp1216
    /*
    得到如表:
    id    firstcol    lastcol     RuntimeValue    xh
    1 b (d+e) d+e 1
    2 a (b+c) b+c 1
    3 d (f-g) f-g 1

    */
    select * from #temp
    declare @I int,@J int,@firstcol varchar(100),@lastcol varchar(100)
    select @J=0
    select @I=2-----此处更正!!
    while @J<>(select max(len(runtimeValue)) from #temp)
    begin
           print @i
    select @j=max(len(runtimeValue)) from #temp
    DECLARE Updatetemp CURSOR local FOR 
    SELECT firstcol,lastcol
    FROM #temp
    OPEN Updatetemp FETCH NEXT FROM Updatetemp
    INTO @firstcol,@lastcol
    WHILE @@FETCH_STATUS = 0
    Begin
    update #temp set runtimeValue=replace(runtimevalue,@firstcol,@lastcol) FETCH NEXT FROM Updatetemp
    INTO @firstcol,@lastcol
    End CLOSE Updatetemp
    DEALLOCATE Updatetemp select @I=@I+1
    End
    update #temp set xh=len(runtimevalue)-len(replace(runtimevalue,'(',''))+1select * from #temp
    ---------------
    1 b (d+e) (f-g)+e 2
    2 a (b+c) ((f-g)+e)+c 3
    3 d (f-g) f-g 1
      

  2.   

    to CrazyFor(Fan):
    基础比较差点,不是很明白,能讲下你的思路吗?谢谢!