2015-03-03 82 views
0

我有一个游标声明的位置:为什么在这个游标声明上没有括号?

declare c cursor 
for  (select ProductName, ListPrice 
     from Products 
     where ListPrice > 700) 

但如果我添加一个order by条款,我得到一个错误:

declare c cursor 
for  (select ProductName, ListPrice 
     from Products 
     where ListPrice > 700 
     order by ListPrice desc) 

错误:Incorrect syntax near the keyword 'order'.

但错误消失,如果我拿掉括号:

declare c cursor 
for  select ProductName, ListPrice 
     from Products 
     where ListPrice > 700 
     order by ListPrice desc 

也许我对于括号在SQL Server中做了些什么不太清楚。是什么赋予了?为什么order by子句以这种方式与圆括号交互?

+0

SQL Server不喜欢子查询中的order by(除非你也有'top')。我想它将括号内的查询解释为一个子查询,因为不需要这些parens。 – 2015-03-03 01:44:36

+0

这很有道理。你应该把答案放在一个答案中,以便我可以给你信用。 – Indigenuity 2015-03-03 02:09:48

回答

0

order by不允许在子查询中使用,如注释中所述。将圆括号中的select包装在一起使得SQL将其解释为子查询。