CREATE OR REPLACE PROCEDURE top_movies IS BEGIN FOR movie_rec IN (SELECT movie_title, movie_avg_rating FROM movie ORDER BY movie_avg_rating DESC FETCH FIRST 5 ROWS ONLY) LOOP DBMS_OUTPUT.PUT_LINE(movie_rec.movie_title || ' - ' || movie_rec.movie_avg_rating); END LOOP; END; Error at line 7: PL/SQL: ORA-00933: SQL command not properly ended
时间: 2023-06-19 19:06:31 浏览: 125
该错误是因为 SQL 查询没有正确结束。您需要在 SELECT 语句的末尾添加分号来正确结束语句。请尝试以下代码:
CREATE OR REPLACE PROCEDURE top_movies
IS
BEGIN
FOR movie_rec IN (
SELECT movie_title, movie_avg_rating
FROM movie
ORDER BY movie_avg_rating DESC
FETCH FIRST 5 ROWS ONLY
)
LOOP
DBMS_OUTPUT.PUT_LINE(movie_rec.movie_title || ' - ' || movie_rec.movie_avg_rating);
END LOOP;
END;
请注意,FETCH FIRST 5 ROWS ONLY 是 Oracle 12c 及更高版本的语法。如果您使用的是旧版本的 Oracle,则可以使用类似于以下查询的语法:
SELECT *
FROM (
SELECT movie_title, movie_avg_rating
FROM movie
ORDER BY movie_avg_rating DESC
)
WHERE ROWNUM <= 5;
阅读全文