gpt4 book ai didi

mysql - SQLFiddle 的存储过程和游标

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:09 25 4
gpt4 key购买 nike

我开发了一个小程序,它实现了一个游标,以根据该部门员工的薪水更新该部门的薪水。我想使用 SQLFiddle 测试这个过程,因为它很方便,但我似乎无法弄清楚如何。我已将下面的代码放在架构中创建表和插入行的下方。我还将查询终止符更改为双斜杠。它看起来如下:

DELIMITER //

CREATE PROCEDURE updateSalary()
BEGIN
DECLARE emp_sal, eDno, dDno INT;
DECLARE dep_cursor SELECT Dno FROM Department;
DECLARE emp_cursor SELECT Dno, Salary FROM Employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;

open dep_cursor;
department_loop: LOOP
FETCH dep_cursor INTO dDno;
IF finished = 1 THEN
LEAVE department;
END IF;

open emp_cursor;

DECLARE total_sum INT DEFAULT 0;
employee_loop: LOOP
FETCH emp_cursor INTO eDno, emp_sal;
IF eDno = dDno THEN
total_sum = total_sum + emp_sal;
END IF;
IF finished = 1 THEN
update department SET total_sal WHERE department.dno = dDno;
LEAVE employee_loop;
END IF;
END LOOP employee_loop;
close emp_cursor;
END LOOP department_loop;
CLOSE dep_cursor;
END

DELIMITER//

我得到的错误是:

Schema Creation Failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER' at line 1: 

我的最后一个问题是,如何在 SQLFiddle 中调用这个存储过程?

谢谢

更新:这是整个架构,我已将其编辑为使用单个斜杠作为终止符,并且还更改了查询终止符。

CREATE TABLE Department (
Dname varchar(80),
Dno int primary key,
Total_sal int,
Manager_ssn int
)/


CREATE TABLE Employee (
Name varchar(80),
SSN int primary key,
Salary int,
Dno int references Department(Dno),
Supervisor_ssn int references Employee(SSN)
)/

ALTER TABLE Department ADD FOREIGN KEY (Manager_ssn) REFERENCES Employee(SSN)/


INSERT INTO Employee VALUES ('John James', 555555555, 5000, NULL , NULL)/
INSERT INTO Department VALUES ('Finance', 20, 7000, 555555555)/
INSERT INTO Department VALUES ('Main', 30, 6000, 555555555)/
INSERT INTO Employee VALUES ('Tammy James', 222222222, 6000, 30, 555555555)/
INSERT INTO Department VALUES ('IT', 10, 3000, 222222222)/
INSERT INTO Employee VALUES ('John Jones', 111111111, 1000, 10, 222222222)/
INSERT INTO Employee VALUES ('Sally Smith', 333333333, 6000, 10, 222222222)/
INSERT INTO Employee VALUES ('Tom Jones', 444444444, 3000, 20, 555555555)/
INSERT INTO Employee VALUES ('Sue Smith', 666666666, 4000, 20, 555555555)/


CREATE PROCEDURE updateSalary()
BEGIN
DECLARE emp_sal, eDno, dDno INT;
DECLARE dep_cursor SELECT Dno FROM Department;
DECLARE emp_cursor SELECT Dno, Salary FROM Employee;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;

open dep_cursor;
department_loop: LOOP
FETCH dep_cursor INTO dDno;
IF finished = 1 THEN
LEAVE department;
END IF;

open emp_cursor;

DECLARE total_sum INT DEFAULT 0;
employee_loop: LOOP
FETCH emp_cursor INTO eDno, emp_sal;
IF eDno = dDno THEN
total_sum = total_sum + emp_sal;
END IF;
IF finished = 1 THEN
update department SET total_sal WHERE department.dno = dDno;
LEAVE employee_loop;
END IF;
END LOOP employee_loop;
close emp_cursor;
END LOOP department_loop;
CLOSE dep_cursor;
END;
/

当我尝试构建架构时收到的新错误是:

Schema Creation Failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Dno FROM Department;
DECLARE emp_cursor SELECT Dno, Salary FROM Employe' at line 4:

最佳答案

使用;以外的查询终止符。
使用 Browser 按钮旁边的左侧第四个按钮选择它。
请点击此链接查看如何在 SqlFiddle 中创建和运行 MySql 程序:http://sqlfiddle.com/#!2/db743/1
我在这个演示中使用了/查询终止符。

CREATE TABLE x( x int )/

INSERT INTO x VALUES(1),(2),(3)/

CREATE PROCEDURE update_salary()
BEGIN
UPDATE x SET x = x + 100;
END;
/

========== 编辑 =================

在遇到语法错误时,我使用了以下过程:

消息说错误在第 4 行:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Dno FROM Department; DECLARE emp_cursor SELECT Dno, Salary FROM Employe' at line 4:

首先我在程序中编号:

1. CREATE PROCEDURE updateSalary()
2. BEGIN
3. DECLARE emp_sal, eDno, dDno INT;
4. DECLARE dep_cursor SELECT Dno FROM Department;

似乎这一行是错误的:DECLARE dep_cursor SELECT Dno FROM Department;

为了检查这一行的语法,我使用了这个检查工具:checking tool
此工具显示 here正确的语法应该是:

DECLARE cursor_name CURSOR FOR select_statement

然后我使用上面的代码更正代码:

 4.   DECLARE dep_cursor CURSOR FOR SELECT Dno FROM Department;

然后再次尝试编译代码。
现在它在第 5 行显示下一个错误:

check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT Dno, Salary FROM Employee; END' at line 5:


所以我对第 5 行重复上述过程......等等,等等。

关于mysql - SQLFiddle 的存储过程和游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23369551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com