sql server ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效

时间:2016-03-07 12:13:53

标签: sql sql-server

    SELECT TOP 28 *
    FROM (
        SELECT DISTINCT [DATECOL]
            ,[CODE ARTICLE]
            ,[CODE SITE]
            ,[CODE STRUCTURE]
        FROM [VENTE] T2
        WHERE PROMO = 0
            AND ([CODE ARTICLE] IS NOT NULL)
            AND ([CODE STRUCTURE] IS NOT NULL)
            AND ([PRIX DE REVIENT] IS NOT NULL)
            AND [CODE ARTICLE] = '    714180291'
            AND [CODE SITE] = '    7'
        ORDER BY [DATECOL]
        ) T

我收到以下错误

  

Msg 1033,Level 15,State 1,Line 9 ORDER BY子句无效   视图,内联函数,派生表,子查询和公用表   表达式,除非还指定了TOP或FOR XML。

如何纠正?

2 个答案:

答案 0 :(得分:1)

只需使用以下代码

即可
    SELECT DISTINCT TOP 28 [DATECOL]
        ,[CODE ARTICLE]
        ,[CODE SITE]
        ,[CODE STRUCTURE]
    FROM [VENTE] T2
    WHERE PROMO = 0
        AND ([CODE ARTICLE] IS NOT NULL)
        AND ([CODE STRUCTURE] IS NOT NULL)
        AND ([PRIX DE REVIENT] IS NOT NULL)
        AND [CODE ARTICLE] = '    714180291'
        AND [CODE SITE] = '    7'
    ORDER BY [DATECOL]

答案 1 :(得分:0)

order by子句应放在外部查询中。使用以下查询

{{1}}