2016-04-17 71 views
0

表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 

我想要寫這需要的XY一個從表1的值作爲程序一個論點,並給出了我最大的id_something我的name我表2。我有這樣的代碼:

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 

我希望你能明白我試圖解釋。

回答

0

你不會(也不能)指定的formal parameters的大小,例如字符串或比例的最大長度/數字的精度。如文檔所述:

您正在聲明的形式參數的數據類型。該數據類型可以是受約束的子類型,但不能包含約束(例如,NUMBER(2)或VARCHAR2(20)

所以聲明應該只是:

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

使用count作爲一個列別名不是一個好主意,因爲它是一個函數名稱,但是你發佈的遊標查詢看起來不錯,所以如果這個別名不會混淆解析器,那麼你可能在更改表名和其他細節。使用max作爲變量名也會引起問題。避免標識符和變量名的保留和關鍵詞。

希望這是一個鍛鍊,你可以做你在普通的SQL想什麼,而無需求助於PL/SQL。

+0

謝謝,我沒有在我的代碼中使用'count',它只是作爲一個解釋,我也嘗試了'VARCHAR2'沒有最大長度,但我用'max'作爲變量名稱,這是問題所在,它現在工作正常。 –