2016-04-17 15 views
0

TABLE1:コンパイルエラー

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の1を取る手順を書きたいです引数は、最も発生したのテーブル3からnameを与えるid_something私はn table2。私はこのコードを持っている:

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)を含むことができない

だから宣言はわずかであるべきである:。

countを使用
CREATE or REPLACE PROCEDURE myprocedure(XYvalue in VARCHAR2) 
is 
... 

列のエイリアスは関数名なので良い考えではありません。あなたが投稿したカーソルクエリはOKです。したがって、そのエイリアスがパーサーを混乱させないならば、テーブル名を変更している間に問題を隠している可能性があります。変数名にmaxを使用すると、問題が発生します。識別子と変数名には予約語とキーワードを使用しないでください。

これは、PL/SQLに頼ることなく、プレーンSQLでしようとしていることを実行できることを望みます。

+0

私のコードで 'count'を使ったことはありませんでした。説明と同じように、VARCHAR2も試してみましたが、変数名として' max'を使用しました。今はうまくいく。 –