繁体   English   中英

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之一作为参数,并从id_something中给出table2中最常出现的id_somethingname 我有以下代码:

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

希望您能理解我要解释的内容。

您没有(也不能)指定形式参数的大小,例如字符串的最大长度或成员的比例/精度。 如文档所述:

您要声明的形式参数的数据类型。 数据类型可以是受约束的子类型,但不能包含约束(例如NUMBER(2)或VARCHAR2(20))。

因此,声明应为:

CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2)
is
...

count用作列别名不是一个好主意,因为它是一个函数名。 不过,您发布的游标查询看起来不错,因此,如果该别名不会使解析器感到困惑,则在更改表名和其他详细信息时您可能已经隐藏了该问题。 使用max作为变量名也会引起问题。 避免使用标识符和变量名的保留关键字。

希望这是一个练习,因为您可以在纯SQL中完成您想做的事情,而不必求助于PL / SQL。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM