LIMIT不适用于UNION

时间:2018-01-21 13:50:53

标签: sql oracle limit

以下代码有什么问题?

SELECT  first_name, last_name
FROM    employees
UNION ALL
SELECT  first_name, last_name
FROM    dependents
ORDER BY  last_name
LIMIT 6 OFFSET 1;

即使是使用LIMIT的简单查询似乎也不起作用:

SELECT  first_name, last_name
FROM    dependents
ORDER BY  last_name
LIMIT 6 OFFSET 1;
  

重复出现此错误:ORA-00933:SQL命令不正确   结束

2 个答案:

答案 0 :(得分:0)

我相信oracle不支持LIMIT。试试这个

ORDER BY last_name
OFFSET 1 ROWS FETCH NEXT 6 ROWS ONLY;

请注意Oracle 12c

支持此语法

答案 1 :(得分:0)

代码有什么问题,Oracle不支持limit。期。最新版本支持ANSI标准fetch first <n> rows only

也许你打算:

SELECT e.*
FROM (SELECT e.*, ROW_NUMBER() OVER (ORDER BY last_name) as seqnum
      FROM (SELECT first_name, last_name FROM employees
            UNION ALL
            SELECT first_name, last_name
            FROM dependents
           ) e
      ORDER BY last_name
     ) e
WHERE seqnum BETWEEN 1 and 7;