加上order by后语法是通不过的。如何既排序又UNION ALL?
declare @tb table (id nvarchar(10))
insert @tb 
select 1 union all
select 2select id from @tb --order by id desc
union all
select 'a'
目标结果
id
2
1
a

解决方案 »

  1.   

    select top 100 percent id from @tb order by id desc
    union all
    select 'a'
      

  2.   


    select id from @tbunion all  
    select 'a' 
    order by id desc  
    --union后order by字段放最后的
      

  3.   


    select id from @tb 
    union all  
    select 'a' 
    order by id desc 
      

  4.   

    如果你的id是字符型要转换下。
    select ltrim(id) as id from @tb 
    union all  
    select 'a' 
    order by 1 desc
      

  5.   

    select * from
    (select id from @tb order by id desc) t  
    union all  select 'a' 
      

  6.   

    select * from(
    select id from @tb 
    union all 
    select 'a'
    ) order by id desc  
      

  7.   

    declare @tb table (id nvarchar(10))  
    insert @tb   
    select 1 union all  
    select 2    SELECT id FROM
    (
    select orderField = 0, id from @tb 
    union ALL
    select orderField = 1, 'a'
    ) t
    order by orderField, id desc  
    /*
    id
    2
    1
    a
    */
      

  8.   

    除了wwwwgou,其他都不符合要求。原因大多如下:
    1.语法都通不过。
    2.我这里只是用1,2,a举例,不要只为例子做文章,要考虑其他场景,例如1,B,2,+A。
    3.根本不符合目标结果。谢谢各位的参与。