select a1 as a_all from table union select a2 as a_all from table union select a3 as a_all from table
建一个循环,将SQL写成一个字符串,然后执行它for i=1 to n do select @string =@string+'select a'+convert(char(10),i)+ ' as a_all from '+convert(char(10),i)exec(@string)
select a1 as a_all from table union all select a2 as a_all from table union all select a3 as a_all from table union all select a4 as a_all from table union all select a5 as a_all from table
union
select a2 as a_all from table
union
select a3 as a_all from table
select @string =@string+'select a'+convert(char(10),i)+
' as a_all from '+convert(char(10),i)exec(@string)
union all
select a2 as a_all from table
union all
select a3 as a_all from table
union all
select a4 as a_all from table
union all
select a5 as a_all from table