.....
while @au_id111<=(select max(auto_id) from hl_map_tl_p3)
begin
set @ho_id='null'
set @tcc_id='null'
set @of_file111=(select of_file from hl_map_tl_p3 where auto_id=@au_id111)
set @ho_id=(select holink_id from ho_link_p3 where (of_file=@of_file111 and from_hoid=(select from_hoid from hl_map_tl_p3 where auto_id=@au_id111) and to_hoid=(select to_hoid from hl_map_tl_p3 where auto_id=@au_id111)))
set @tcc_id=(select [id] from link_all where (fnode=(select from_tccnode from hl_map_tl_p3 where auto_id=@au_id111) and tnode=(select to_tccnode from hl_map_tl_p3 where auto_id=@au_id111)))
set @au_id1111=@au_id111
set @sql='update hl_map_tl_p3_'+@sub+ ' set holink_id= '''+@ho_id+''' where auto_id= '''+@au_id1111+''''
exec(@sql)
set @sql='update hl_map_tl_p3_'+@sub+ ' set tcclink_id= '''+@tcc_id+''' where auto_id= '''+@au_id1111+''''
exec(@sql)
set @au_id111=@au_id111+1
continue
end
....问题是这样的:while @au_id111<=(select max(auto_id) from hl_map_tl_p3)中的hl_map_tl_p3表名现在成了一个动态表名'hl_map_tl_p3'+@sub,@sub是个字符串,上面的代码是写在存储过程里的,所以while循环应该是这样写:set @sql='while @au_id111<=(select max(auto_id) from hl_map_tl_p3_'+@sub+')'   exec(@sql)  可是也不对 不知道该这么解决了 请高手帮帮忙 谢谢了

解决方案 »

  1.   

    select max(auto_id) from hl_map_tl_p3把上面一截查询用函数来实现,通过EXEC()语句来执行。
    再通过函数返回的数值作为循环的条件。
      

  2.   

    set @sql='while @au_id111<=(select max(auto_id) from hl_map_tl_p3_'+@sub+')'   
    exec(@sql) ------------------
    --想了一個笨方法...或許實現是可以實現,不過就是搓了一點^*^declare @temp_id int
    exec('select max(auto_id) as auto_id into temp_t from hl_map_tl_p3_'+@sub)
    set @temp_id=(select auto_id from temp_t)
    drop table temp_t
    while @au_id111<=@temp_id
    ...