簡體   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