我已经写了一段代码: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
我在这里错过了什么? 非常感谢你
通过切断你所做的错误信息,你已经消除了最有意义的部分。 – Uueerdo
附近是什么?..显示其余的消息..是最好的部分 – scaisEdge
我注意到的第一件事是只有第一个变量具有所需的'DECLARE'。 _另外,在游标中使用'SELECT *'通常是不好的形式,但是由于您已经在proc中创建了表格,所以它并不是完全可怕的。0 – Uueerdo