我正在寻找"ORDER BY RAND()"和"ORDER BY NEWID()"的官方T-SQL文档.有许多文章描述它们,因此必须在某处记录它们.
我正在寻找像这样的官方SQL Server文档页面的链接:http: //technet.microsoft.com/en-us/library/ms188385.aspx
澄清:
我正在寻找的是"order_by_expression"的文档,它解释了非负整数常量,返回非负整数的函数和返回任何其他值的函数(如RAND()或NEWID())之间的行为差异. ).
答案:
我为我原来的问题缺乏明确性而道歉.与大多数与编程相关的问题一样,问题的解决方案主要是弄清楚您实际上想要回答的问题.
谢谢大家.
答案在本文档中:来自:http://www.wiscorp.com/sql200n.zip
Information technology — Database languages — SQL — Part 2: Foundation (SQL/Foundation)
22.2 <direct select statement: multiple rows> includes a <cursor specification>.
Run Code Online (Sandbox Code Playgroud)
此时我们得到了答案的前半部分:
SELECT语句是一种CURSOR,这意味着可以在每一行上迭代执行操作.虽然我没有在明确说明的文档中找到声明,但我很乐意假设order_by_expression 中的表达式将针对每一行执行.
现在,当您使用RAND()或NEWID()或CEILING(RAND()+ .5)/ 2而不是数字常量或列名称时,会发生什么.
该表达式永远不会被视为列号.它将始终是为每行生成的值,它将用作确定行顺序的基础.
但是,为了彻底性,让我们继续完整定义表达式.
14.3 <cursor specification> includes ORDER BY <sort specification list>.
10.10 <sort specification list> defines:
<sort specification> ::= <sort key> [ <ordering specification> ] [ …
Run Code Online (Sandbox Code Playgroud) 我的目标是从表中登记所有元素,cat.CAT_RUTAS
然后我将null
值添加到列表中,其值为'Enlistar todas las rutas',所以我希望该null
值成为结果的第一个选项并对其余值进行排序在提升.
我正在使用MSSQL SERVER但是当我尝试运行此查询时:
select CATrut_iIdentificador, CATrut_vDescripcion
from cat.CAT_Rutas
UNION
SELECT NULL , 'Enlistar todas las rutas'
order by CATrut_vDescripcion ASC NULLS FIRST
Run Code Online (Sandbox Code Playgroud)
问题是当我试图将空值添加到顶部时.收到错误:
'NULLS'附近的语法不正确.
样品:
var aux = new int[] { -1,0,1,-1,2,3,4,5,6,7 }
Run Code Online (Sandbox Code Playgroud)
预期结果:
{ 2,3,4,5,6,7,1,0,-1,-1 }
Run Code Online (Sandbox Code Playgroud)
怎么样?
-
编辑:抱歉这个糟糕的问题.我想从值2和其他放在较低端的人(我修复文本)中订购所有.