【问题标题】:I am facing a problem regarding limiting my rows我在限制行数方面遇到问题
【发布时间】:2021-01-06 18:47:13
【问题描述】:
DECLARE
    cursor c is
    select book.bookid,book.title,publisher.publishername,
    author.firstname,author.lastname from book 
    inner join publisher on 
    book.publisherid  =publisher.publisherid
    inner join author on book.author_id=author.authorid
    order by book.title
    fetch next 2 rows only;
BEGIN
    dbms_output.put_line('BOOKID..TITLE..publisher..Author');
    for i in c
        loop
            dbms_output.put_line(i.bookid||'..'||substr(i.title,1,
            10)||'..'||i.publishername||'..'||i.firstname||' '||i.lastname);
        end loop;
end;
/

我在“仅获取接下来的 2 行”这一行上遇到错误。你能解释一下并给我正确的解决方案吗?我只想要前 2 行。

错误- 限制 2; * 第 9 行的错误: ORA-06550:第 9 行,第 5 列: PL/SQL: ORA-00933: SQL 命令未正确结束 ORA-06550:第 3 行,第 5 列: PL/SQL:忽略 SQL 语句

【问题讨论】:

  • 你遇到了什么错误?
  • 您运行的是哪个版本的 Oracle?
  • 究竟是什么错误?
  • 请看一下。
  • 看来你不是Oracle 12c版本,这里必须使用rownum概念。

标签: oracle plsql


【解决方案1】:

尝试改用WHERE ROWNUM <= 2。它需要包装原始查询,以便在返回前两行之前考虑排序。

DECLARE
    CURSOR c IS
        SELECT *
          FROM (  SELECT book.bookid,
                         book.title,
                         publisher.publishername,
                         author.firstname,
                         author.lastname
                    FROM book
                         INNER JOIN publisher ON book.publisherid = publisher.publisherid
                         INNER JOIN author ON book.author_id = author.authorid
                ORDER BY book.title)
         WHERE ROWNUM <= 2;
BEGIN
    DBMS_OUTPUT.put_line ('BOOKID..TITLE..publisher..Author');

    FOR i IN c
    LOOP
        DBMS_OUTPUT.put_line (
               i.bookid
            || '..'
            || SUBSTR (i.title, 1, 10)
            || '..'
            || i.publishername
            || '..'
            || i.firstname
            || ' '
            || i.lastname);
    END LOOP;
END;
/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-26
    • 2021-12-24
    • 2013-01-23
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多