sqlserver sql 异常<除非另外还指定了 TOP、OFFSET 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。>

问题:使用sql查询时报错

SELECT
    *
FROM
    (
        SELECT
            a.value2,
            a.value3,
            a.value4,
            a.hospitalization_id
        FROM
            v_treatment_process a
        WHERE
            a.is_del = 0
        AND a.value1 = '1'
        ORDER BY
            a.treatment_items,
            a.use_func,
            a.is_record,
            a.value2,
            a.value3 DESC
    ) d
WHERE
    1 = 1

解决办法:在sql子查询SELECT 后加 TOP 100 PERCENT (查询出前百分比为100的数据,也就是查询出全部数据) ,如下

SELECT
    *
FROM
    (
        SELECT TOP 100 PERCENT 
            a.value2,
            a.value3,
            a.value4,
            a.hospitalization_id
        FROM
            v_treatment_process a
        WHERE
            a.is_del = 0
        AND a.value1 = '1'
        ORDER BY
            a.treatment_items,
            a.use_func,
            a.is_record,
            a.value2,
            a.value3 DESC
    ) d
WHERE
    1 = 1

你可能感兴趣的:(数据库,sqlserver,sql,xml)