使用SQLFiddle存储过程和游标

时间:2014-04-29 15:54:08

标签: mysql

我开发了一个小程序,它实现了一个游标,根据该部门员工的工资来更新部门的工资。我想用SQLFiddle测试这个程序只是因为它非常方便,但我似乎无法弄清楚如何。我已将下面的代码放在Schema中的Create Table和Insert行下面。我还将查询终止符更改为双斜杠。它看起来如下:

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: 

1 个答案:

答案 0 :(得分:3)

使用;以外的查询终止符 使用左侧第四个按钮,在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行:

  

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在'SELECT Dno FROM Department附近使用正确的语法;     DECLARE emp_cursor SELECT Dno,工资来自员工'第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行的下一个错误:

  

检查与您的MySQL服务器版本对应的手册,以便在'SELECT Dno,Salary FROM Employee附近使用正确的语法; END'第5行


所以我重复上面第5行的程序....依此类推,等等。