简体   繁体   English

sql过程中的编译错误

[英]Compile error in sql procedure

table1: 表格1:

id_client | XY 
--------------
01        | str1 
02        | str2 
03        | str1 

table2: 表2:

id_client | id_something
-------------------
02        | 32
02        | 48
01        | 32

table3: 表3:

id_something | name
--------------------
48           | john
32           | george

I want to write a procedure which takes one of XY from table1 values as an argument and gives the name from table3 of the most occured id_something in table2. 我想编写一个过程,该过程将table1值中的XY之一作为参数,并从id_something中给出table2中最常出现的id_somethingname I have this code: 我有以下代码:

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;
  /

And this is the error which a got and can't figure out what is the problem: 这是一个无法解决的错误:

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

I hope you will understand what I am trying to explain. 希望您能理解我要解释的内容。

You don't (and can't) specify the size of formal parameters , eg max length of string or scale/precision of mumbers. 您没有(也不能)指定形式参数的大小,例如字符串的最大长度或成员的比例/精度。 As the documentation says: 如文档所述:

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). 数据类型可以是受约束的子类型,但不能包含约束(例如NUMBER(2)或VARCHAR2(20))。

So the declaration should just be: 因此,声明应为:

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

Using count as a column alias isn't a good idea as it's a function name. count用作列别名不是一个好主意,因为它是一个函数名。 The cursor query you've posted looks OK though, so if that alias isn't confusing the parser then you may have hidden the issue while changing your table names and other details. 不过,您发布的游标查询看起来不错,因此,如果该别名不会使解析器感到困惑,则在更改表名和其他详细信息时您可能已经隐藏了该问题。 Using max as a variable name will also cause problems. 使用max作为变量名也会引起问题。 Avoid reserved and key words for identifiers and variable names. 避免使用标识符和变量名的保留关键字。

Hopefully this is an exercise as you can do what you're trying in plain SQL, without needing to resort to PL/SQL. 希望这是一个练习,因为您可以在纯SQL中完成您想做的事情,而不必求助于PL / SQL。

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

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