SQL Server事务,订购选择结果集时出错

时间:2017-11-21 17:56:08

标签: sql-server

我无法订购从事务返回的结果集。

我收到错误:

  

关键字' ORDER'附近的语法不正确。

我的代码:

IF EXISTS (SELECT MenuItemID
           FROM dbo.Menu_Items_Custom
           WHERE PodID = 288 )
BEGIN
    (SELECT 
         C.MenuItemID,
         ISNULL(NewName, ItemName) AS ItemName,
         ISNULL(URL, ItemURL) AS ItemURL,
         M.Type,
         C.OrderBy
     FROM      
         Menu_Items_Custom C
     LEFT JOIN 
         Menu_Items M ON M.MenuItemID = C.MenuItemID
     WHERE     
         (ChildOf IS NULL AND PodID = 288)
     ORDER BY  
         C.OrderBy);
END;
ELSE
BEGIN
    (SELECT *
     FROM dbo.Menu_Items
     WHERE ChildOf IS NULL);
END;

1 个答案:

答案 0 :(得分:2)

无需将SELECT括在括号中,也可以删除2个额外的分号:

IF EXISTS ( SELECT  MenuItemID
            FROM    dbo.Menu_Items_Custom
            WHERE   PodID = 288 )
    BEGIN
        SELECT    C.MenuItemID ,
                    ISNULL(NewName, ItemName) AS ItemName ,
                    ISNULL(URL, ItemURL) AS ItemURL ,
                    M.Type ,
                    C.OrderBy
          FROM      Menu_Items_Custom C
                    LEFT JOIN Menu_Items M ON M.MenuItemID = C.MenuItemID
          WHERE     (ChildOf IS NULL
                    AND PodID = 288)
          ORDER BY  C.OrderBy

    END
ELSE
    BEGIN
        SELECT    *
        FROM      dbo.Menu_Items
        WHERE     ChildOf IS NULL
    END;