错误(8,1):PLS-00103:遇到符号“SET”
Posted
技术标签:
【中文标题】错误(8,1):PLS-00103:遇到符号“SET”【英文标题】:Error(8,1): PLS-00103: Encountered the symbol "SET" 【发布时间】:2021-01-14 18:03:59 【问题描述】:我正在尝试使用 oracle 创建一个函数,它应该可以正常工作,但我不断收到以下错误:
这是我的功能:
CREATE OR REPLACE FUNCTION CountViewers(nameofPlay plays.play%TYPE) RETURN NUMBER AS
NUM NUMBER;
BEGIN
SELECT SUM(registerd) INTO num1 FROM plays WHERE play=nameofPlay;
return num;
END;
SET SERVEROUT ON;
DECLARE
inc integer;
res NUMBER;
invalid_status EXCEPTION;
CURSOR clients2 IS SELECT * FROM plays;
name_of_play plays.play%ROWTYPE;
play_name plays%TYPE;
BEGIN
inc:=0;
name_of_play := '&Play';
OPEN clients2;
LOOP
FETCH clients2 INTO play_name;
IF name_of_play != play_name.play and clients2%rowcount>i THEN
inc:=inc+1;
ELSIF name_of_play = play_name.play THEN
EXIT WHEN clients2%found;
ELSE
RAISE invalid_status;
--like throw error
END IF;
EXIT WHEN clients2%notfound;
END LOOP;
res:=CountViewers(name_of_play);
DBMS_OUTPUT.PUT_LINE(name_of_play ||' | '|| res);
EXCEPTION
WHEN invalid_status THEN DBMS_OUTPUT.PUT_LINE('The name of play is not found');
END;
【问题讨论】:
这能回答你的问题吗? Error(36,1): PLS-00103: Encountered the symbol "SET" 作为初学者,您是否编写了所有代码,在此过程中甚至没有尝试执行一次?!? @jarlh 问题出在哪里?! @odaiwa - 链接问题中的一个答案涵盖了直接问题:您在函数创建和脚本的其余部分之间缺少斜线 (/)。 ;请参阅文档中的how to run PL/SQL blocks,假设您使用的是 SQL*Plus、SQL Developer 或 SQLcl。 【参考方案1】:问题是你把这个命令放在哪里:SET SERVEROUT ON;
导致它在函数声明中。
你应该试着把它带到函数之外:
SET SERVEROUT ON;
\
CREATE OR REPLACE FUNCTION CountViewers(nameofPlay plays.play%TYPE) RETURN NUMBER AS
NUM NUMBER;
BEGIN
SELECT SUM(registerd) INTO num1 FROM plays WHERE play=nameofPlay;
return num;
END;
DECLARE
inc integer;
res NUMBER;
invalid_status EXCEPTION;
CURSOR clients2 IS SELECT * FROM plays;
name_of_play plays.play%ROWTYPE;
play_name plays%TYPE;
BEGIN
inc:=0;
name_of_play := '&Play';
OPEN clients2;
LOOP
FETCH clients2 INTO play_name;
IF name_of_play != play_name.play and clients2%rowcount>i THEN
inc:=inc+1;
ELSIF name_of_play = play_name.play THEN
EXIT WHEN clients2%found;
ELSE
RAISE invalid_status;
--like throw error
END IF;
EXIT WHEN clients2%notfound;
END LOOP;
res:=CountViewers(name_of_play);
DBMS_OUTPUT.PUT_LINE(name_of_play ||' | '|| res);
EXCEPTION
WHEN invalid_status THEN DBMS_OUTPUT.PUT_LINE('The name of play is not found');
END;
【讨论】:
以上是关于错误(8,1):PLS-00103:遇到符号“SET”的主要内容,如果未能解决你的问题,请参考以下文章
错误(5,1):PLS-00103:创建函数时遇到符号“CREATE”错误