2016-08-12 86 views
1

我已經寫了一段代碼:SQL代碼不工作,未知錯誤

CREATE PROCEDURE test() 
BEGIN 
    DECLARE 
    ok INT default FALSE; 
    curs_r1 CURSOR FOR SELECT * FROM t WHERE (b > 1 and b < 3) and (c < 2); 
    curs_r2 CURSOR FOR SELECT * FROM t WHERE (a = 1) and (b > 2); 
    CONTINUE HANDLER FOR NOT FOUND SET ok = TRUE; 


    SET ok = False; 
    DROP TABLE IF EXISTS t; 
    CREATE TABLE IF NOT EXISTS t (
    id int, 
    a int, 
    b int, 
    c int 
); 
    DROP TABLE IF EXISTS res; 
    CREATE TABLE IF NOT EXISTS res (
    id int not null unique, 
    score float 
); 

    insert into t values (0,1,2,3), (1,1,3,2), (2,3,2,1); 

    -------------------------------------------- 
    OPEN curs_r1; 
    SET score_r1 = 0.5; 
    REPEAT 
    FETCH curs_r1 INTO 
    id, a, b, c; 
    INSERT IGNORE INTO res VALUES (id, score_r1); 
    UNTIL ok END REPEAT; 
    CLOSE curs_r1; 

    -------------------------------------------- 
    SET ok = FALSE; 

    OPEN curs_r2; 
    SET score_r2 = 0.25; 
    REPEAT 
    FETCH curs_r2 INTO 
    id, a, b, c; 
    INSERT IGNORE INTO res VALUES (id, score_r2); 
    UNTIL ok END REPEAT; 
    CLOSE curs_r2; 



    SELECT * FROM res; 
END 

但這產生類似的錯誤太多:

Error: near line 1: near "PROCEDURE": syntax Error

Error: near line 5: near "curs_r1": syntax Error

Error: near line 6: near "curs_r2": syntax Error

Error: near line 7: near "CONTINUE": syntax Error

Error: near line 10: near "SET": syntax Error

Error: near line 27: near "OPEN": syntax Error

Error: near line 28: near "SET": syntax Error

Error: near line 29: near "REPEAT": syntax Error

Error: near line 32: near "IGNORE": syntax Error

Error: near line 33: near "UNTIL": syntax Error

Error: near line 34: near "CLOSE": syntax Error

Error: near line 37: near "SET": syntax Error

Error: near line 39: near "OPEN": syntax Error

Error: near line 40: near "SET": syntax Error

Error: near line 41: near "REPEAT": syntax Error

Error: near line 44: near "IGNORE": syntax Error

Error: near line 45: near "UNTIL": syntax Error

Error: near line 46: near "CLOSE": syntax Error

Error: incomplete SQL: END

沒有人有根源的想法請?

非常感謝你提前。


以上,其中固定的錯誤(感謝你達爾文的第一個線索),這裏是新的一段代碼:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS test; 
CREATE PROCEDURE test() 
BEGIN 
    DECLARE ok INT default FALSE; 
    DECLARE score_r1 FLOAT default 0.5; 
    DECLARE score_r2 FLOAT default 0.25; 
    DECLARE id, a, b, c INT; 
    DECLARE curs_r1 CURSOR FOR SELECT * FROM t WHERE (b > 1 and b < 3) and (c < 2); 
    DECLARE curs_r2 CURSOR FOR SELECT * FROM t WHERE (a = 1) and (b > 2); 
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET ok = TRUE; 

    SET ok = False; 
    DROP TABLE IF EXISTS t; 
    CREATE TABLE IF NOT EXISTS t (
    id int, 
    a int, 
    b int, 
    c int 
); 
    DROP TABLE IF EXISTS res; 
    CREATE TABLE IF NOT EXISTS res (
    id int not null unique, 
    score float 
); 

    insert into t values (0,1,2,3), (1,1,3,2), (2,3,2,1); 

    OPEN curs_r1; 
    OPEN curs_r2; 

    REPEAT 
    FETCH curs_r1 INTO id, a, b, c; 
    INSERT IGNORE INTO res VALUES (id, score_r1); 
    UNTIL ok END REPEAT; 
    CLOSE curs_r1; 

    SET ok = FALSE; 


    REPEAT 
    FETCH curs_r2 INTO id, a, b, c; 
    INSERT IGNORE INTO res VALUES (id, score_r2); 
    UNTIL ok END REPEAT; 
    CLOSE curs_r2; 

    SELECT * FROM res; 
END; 
$$ 
DELIMITER ; 
輸出

,我期待看到兩行編號= 2和id = 1 但我只有一行ID = 0和得分= 0.5

我在這裏錯過了什麼? 非常感謝你

+2

通過切斷你所做的錯誤信息,你已經消除了最有意義的部分。 – Uueerdo

+2

附近是什麼?..顯示其餘的消息..是最好的部分 – scaisEdge

+0

我注意到的第一件事是隻有第一個變量具有所需的'DECLARE'。 _另外,在遊標中使用'SELECT *'通常是不好的形式,但是由於您已經在proc中創建了表格,所以它並不是完全可怕的。0 – Uueerdo

回答

2

所以,基本上你會得到每條語句的錯誤信息。爲了建立從mysql客戶端程序,您必須DELIMITER指令內包圍整個CREATE聲明,就像這樣:

DELIMITER $$ 
CREATE PROCEDURE test() 
    BEGIN 
    ... --code body goes here 
    END; 
$$ 
DELIMITER ; 

你做你的代碼有許多其他錯誤,但這樣做解決了第一個。

有關DELIMITER的更多信息,請搜索Stack Overflow存檔。

+0

感謝您的第一條提示,解除阻止我幫助我調試了其餘部分。你能否再看看我的編輯?非常感謝 – dark

+0

現在沒問題,改變變量名稱就可以修復它。謝謝 – dark

+0

大多數人看起來都不錯,但我注意到你沒有對你從光標處獲取的數據做任何事情。 –