select IDENTITY(int,1,1) as idx,tb.AA,tb.BB,tb.CC<br>into #temp<br>from table_name tb<br><br>declare @sqlselectstr varchar(1000),@sqlfromstr varchar(1000),@max_idx int,@counter int,@pre_alias varchar(10),@new_alias varchar(10)<br>select @sqlselectstr='select t1.AA,t1.BB,t1.CC ',@sqlfromstr = ' from #temp t1 ',@counter = 1, @max_idx = max(idx),@pre_alias = 't1' <br>from #temp<br><br>if @max_idx is not null<br>while(@counter< @max_idx)<br>begin<br> select @counter = @counter + 1<br> select @new_alias = 't'+convert(varchar,@counter)<br> select @sqlselectstr = @sqlselectstr + ',' + @new_alias +'.AA,'+ @new_alias +'.BB,'+ @new_alias +'.CC'<br> select @sqlfromstr = @sqlfromstr + ' join #temp '+ @new_alias + ' on ' + @pre_alias +'.idx+1 = '+ @new_alias + '.idx'<br> select @pre_alias = @new_alias<br>end<br>exec (@sqlselectstr + @sqlfromstr)<br><br>-----------<br>This script is not so good when there are many records in the table.