声明语句中的ORDERBY'不正确的语法'

时间:2014-06-23 18:47:10

标签: sql sql-server sql-server-2008 sql-order-by

我试图做一个如此简单的任务,但它让我疯狂如何不断失败。

DECLARE @neighbour HierarchyId = (SELECT [stPath] as tmpPath
                                  FROM [DEV].[tmp].[StrategyTable] t  
                                  WHERE ParentCode = 'TOP') 
                                  ORDER BY t.stKey DESC;

我不断收到错误

Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'ORDER'.

同时,它适用于没有where子句的东西;像

DECLARE @parent HierarchyId = (SELECT 
                            [stPath] AS tmpPath
                            FROM [DEV].[tmp].[StrategyTable] t WHERE [ParentCode] = 'TOP')

WTF .......

1 个答案:

答案 0 :(得分:1)

DECLARE @neighbour HierarchyId = (SELECT TOP 1 [stPath] as tmpPath
                                  FROM [DEV].[tmp].[StrategyTable] t  
                                  WHERE ParentCode = 'TOP' 
                                  ORDER BY t.stKey DESC);

这应该可以达到你想要的效果。