作为一个经常编写SQL语句的人员,有时候需要进行order by 以后再 order by排序结果集数据,虽然从语句来说没有问题,但是执行的时候会报错,错误提示为:

SQL“除非另外还指定了 TOP、OFFSET 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效“

2024-02-28T12:01:14.png

解决方法:
执行sql语句:

select * from (

select * from tab order by userID desc

) as a order by date desc

逻辑上看着挺对 但是报错:

除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。

只要我们在嵌套子查询视图里面加入: top 100 percent 即可

select * from (

select top 100 percent * from tab order by userID desc

) as a order by date desc

发表评论