为什么这个游标声明没有括号?

时间:2015-03-03 01:30:26

标签: sql sql-server-2012 syntax-error cursors

我在这里有一个游标声明:

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子句会以这种方式与括号相互作用?

1 个答案:

答案 0 :(得分:0)

如评论中所述,子查询中不允许

order by。将select括在括号中会使SQL将其解释为子查询。

相关问题