一条查询语句包括联表,排序,分组,等功能,如:
select * from
(
select * from table_a
      inner join table_b
            on table_a.ID = table_b.id
where table_a.name like '%a%'
order by table_a
group by table_b.Name
) table_test
where id not id ('2','4','5',)等等,总之这条语句很复杂,我想将这个查询结果当做一个集合来进行二次固定功能的过虑,如分页,前提是这个集合是未知的,可能存在N种情况,于是我这样写select * from
( 复杂的SQL语句 ) table_temp一旦‘复杂且未知的SQL语句’中有Order by 就会报错,除非同时指定了 TOP,否则 ORDER BY 子句在视图、内嵌函数、派生表和子查询中无效。 , 只有将‘复杂且未知的SQL语句’中的Order By提取出来放到最外面得集合才不会报错,难不成要分析传过来的‘复杂且未知的SQL语句’ ? 有没有简便一些的办法可以获取到这个动态的集合呢?

解决方案 »

  1.   

    哥们,如果你对SQL语句一无所知的话,建议重头学起吧.
    你的语句里都没有聚合函数,做group by干嘛?
    为什么要把过滤条件in(...)放到外面来,好玩儿吗?
      

  2.   

    select * from
    (
    select * from table_a
      inner join table_b
      on table_a.ID = table_b.id
    where table_a.name like '%a%'
    order by table_a
    group by table_b.Name
    ) table_test
    where id not id ('2','4','5',)红色部分不需要。
      

  3.   

    group by table_b.Name
    ) table_test
    where id not id ('2','4','5',)红色部分多余,蓝色部分打错。
      

  4.   

    而且这个查询不觉得很繁琐么?明明可以一条SQL语句搞出来的,怎么就要用子查询了select * from table_a
      inner join table_b
      on table_a.ID = table_b.id
    where table_a.name like '%a%' and tablea.id not in (2,4,5)猜测id也是int型吧!如果是子查询,两个表都有id,需要给某个id列起别名的。
      

  5.   


    这个SQL语句是未知的,只能尽量往复杂去考虑,
      

  6.   

    这个复杂的SQL语句不重要,重点是我要对这个未知的SQL语句进行二次过虑.
      

  7.   


    --子查询的表这样 
    select top 100 percent * from tb
      

  8.   

    --子查询的表这样 
    select top 100 percent * from tb order by id