日期:2014-05-18  浏览次数:20459 次

使用sql server System.Data.SqlClient.SqlCommand,为何Order By不起任何作用
在网上查的写法

SELECT * FROM ( SELECT TOP (100) PERCENT * FROM myTable ORDER BY myTable.uid DESC) as newtable

能够返回结果,但是ORDER BY是不起任何作用的,无论是 ASC 还是 DECS,

而且如果直接写SELECT * FROM myTable ORDER BY myTable.uid DESC 赋值给System.Data.SqlClient.SqlCommand.CommandText,
会报错SQLException: Column 'xxx' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

烦死了,急求解答

------解决方案--------------------
SELECT * FROM ( SELECT TOP (100) percent * FROM myTable ORDER BY myTable.uid DESC) as newtable order by myTable.uid DESC
------解决方案--------------------
newTable.uid有这个字段不
------解决方案--------------------
在sql server的查询编辑器里好像没错哦。
------解决方案--------------------
因为你用了TOP (100) PERCENT,这是返回所有数据,外查询还是根据你的主键或者默认数据来显示的
可以把order by放在外面,或者外面再用一次order by 或者改Top n (n<100)


SELECT * FROM ( SELECT TOP (100) percent * FROM myTable ORDER BY myTable.uid DESC) as newtable order by myTable.uid DESC
这个错误的原因是外查询表名并不是myTable了,当然不存在myTable.uid