sql 过程中的编译错误
Compile error in sql procedure
表 1:
id_client | XY
--------------
01 | str1
02 | str2
03 | str1
表 2:
id_client | id_something
-------------------
02 | 32
02 | 48
01 | 32
表 3:
id_something | name
--------------------
48 | john
32 | george
我想编写一个程序,它将 table1 值中的 XY
之一作为参数,并给出 table3 中 table2 中出现次数最多的 id_something
中的 name
。我有这个代码:
CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2(100))
is
cursor countsCursor is select id_something, count(*) count
from table1 join table2 using (id_client)
WHERE XY=XYvalue
group by id_something;
cnt countsCursor%ROWTYPE;
max NUMBER;
idMax table2.id_something%TYPE;
maxName table3.name%TYPE;
BEGIN
max := 0;
open countsCursor;
loop
fetch countsCursor into cnt;
exit when countsCursor%NOTFOUND;
IF (cnt.count > max) THEN
max := cnt.count;
idMax := cnt.id_something;
END IF;
END loop;
select name into maxName from table3 where id_something = idMax;
if (max = 0) THEN
dbms_output.put_line('No id found');
else
dbms_output.put_line('Most occured is ' || maxName || ', with count: ' || max || '.');
END;
/
这是一个无法弄清楚问题所在的错误:
1/59 PLS-00103: Encountered the symbol "(" when expecting one of the following:
:= . ) , @ % default character
The symbol ":=" was substituted for "(" to continue.
3/71 PLS-00103: Encountered the symbol "JOIN" when expecting one of the following:
, ; for group having intersect minus order start union where
connect
希望您能理解我要解释的内容。
您没有(也不能)指定 formal parameters 的大小,例如字符串或数字的最大长度 scale/precision。正如文档所说:
Data type of the formal parameter that you are declaring. The data type can be a constrained subtype, but cannot include a constraint (for example, NUMBER(2) or VARCHAR2(20).
所以声明应该只是:
CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2)
is
...
使用 count
作为列别名不是一个好主意,因为它是一个函数名。不过,您发布的游标查询看起来没问题,所以如果该别名没有混淆解析器,那么您可能在更改 table 名称和其他详细信息时隐藏了问题。使用 max
作为变量名也会出现问题。避免标识符和变量名称的保留字和关键字。
希望这是一个练习,因为您可以在普通 SQL 中完成您正在尝试的事情,而无需求助于 PL/SQL。
表 1:
id_client | XY
--------------
01 | str1
02 | str2
03 | str1
表 2:
id_client | id_something
-------------------
02 | 32
02 | 48
01 | 32
表 3:
id_something | name
--------------------
48 | john
32 | george
我想编写一个程序,它将 table1 值中的 XY
之一作为参数,并给出 table3 中 table2 中出现次数最多的 id_something
中的 name
。我有这个代码:
CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2(100))
is
cursor countsCursor is select id_something, count(*) count
from table1 join table2 using (id_client)
WHERE XY=XYvalue
group by id_something;
cnt countsCursor%ROWTYPE;
max NUMBER;
idMax table2.id_something%TYPE;
maxName table3.name%TYPE;
BEGIN
max := 0;
open countsCursor;
loop
fetch countsCursor into cnt;
exit when countsCursor%NOTFOUND;
IF (cnt.count > max) THEN
max := cnt.count;
idMax := cnt.id_something;
END IF;
END loop;
select name into maxName from table3 where id_something = idMax;
if (max = 0) THEN
dbms_output.put_line('No id found');
else
dbms_output.put_line('Most occured is ' || maxName || ', with count: ' || max || '.');
END;
/
这是一个无法弄清楚问题所在的错误:
1/59 PLS-00103: Encountered the symbol "(" when expecting one of the following:
:= . ) , @ % default character
The symbol ":=" was substituted for "(" to continue.
3/71 PLS-00103: Encountered the symbol "JOIN" when expecting one of the following:
, ; for group having intersect minus order start union where
connect
希望您能理解我要解释的内容。
您没有(也不能)指定 formal parameters 的大小,例如字符串或数字的最大长度 scale/precision。正如文档所说:
Data type of the formal parameter that you are declaring. The data type can be a constrained subtype, but cannot include a constraint (for example, NUMBER(2) or VARCHAR2(20).
所以声明应该只是:
CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2)
is
...
使用 count
作为列别名不是一个好主意,因为它是一个函数名。不过,您发布的游标查询看起来没问题,所以如果该别名没有混淆解析器,那么您可能在更改 table 名称和其他详细信息时隐藏了问题。使用 max
作为变量名也会出现问题。避免标识符和变量名称的保留字和关键字。
希望这是一个练习,因为您可以在普通 SQL 中完成您正在尝试的事情,而无需求助于 PL/SQL。