卡在存储过程和光标上

时间:2015-02-04 19:44:24

标签: mysql sql stored-procedures signals cursors

我试图创建一个接受TYPEID&的存储过程。用户号作为参数然后搜索尚未存在的可用条目 已经/已经返回(显示比较并查看表1和2),然后在贷款表中插入一个新行。在此之前检查 如果用户被禁止(表3,思考使用SIGNAL)。

数据:

TABLE1: 
+-----------+----------+
|    CODE   |  TYPEID  |
+-----------+----------+
| 441       |  mn014   |
| 223       |  mn014   |
| 224       |  mn014   |
| 655       |  mn089   |
| 854       |  mn089   |
| 449       |  mn032   |
+-----------+----------+

TABLE2:
+-----------+----------+----------+
| CODE      | TAKENDTE | RTURNDTE |
+-----------+----------+----------+
| 441       | 25/08/14 | 01/01/15 |
| 223       | 25/08/14 | 03/01/15 |
| 223       | 25/08/14 | 01/02/15 |
| 223       | 25/08/14 |   NULL   |
| 655       | 25/08/14 | 07/01/15 |
| 854       | 25/08/14 |   NULL   |
| 449       | 25/08/14 | 06/01/15 |
+-----------+---------------------+

TABLE3:
+-----------+----------+----------+
| USERNO    |   NAME   |  BANNED  |
+-----------+----------+----------+
| 111       |  Bob     |    0     |
| 112       |  Sally   |    1     |
| 113       |  Jim     |    0     |
| 114       |  Billy   |    0     |
| 115       |  Jessica |    0     |
| 116       |  Fred    |    0     |
| 117       |  Patrik  |    0     |
+-----------+---------------------+

我尝试使用CURSOR,这是我认为需要使用的但是我遇到的错误我无法解决这个问题,这是我迄今为止所做的事情

    DELIMITER $$

    CREATE PROCEDURE new_entry(IN isbn_search CHAR(17), useNO INT)

     BEGIN
     DECLARE complete BOOLEAN DEFAULT FALSE;
     DECLARE newEntry VARCHAR (30) DEFAULT ' ';
     DECLARE S CURSOR FOR
    select t1.*
    from table1 t1
    where typeid = 'mn014' and
          not exists (select 1
                      from table2 t2
                      where t2.code = t.code and
                            t2.rturndte is null
                     );      
     DECLARE CONTINUE HANDLER FOR NOT FOUND
       SET complete = TRUE;

                OPEN S;
        SET newLoan = ' ';
        sloop : LOOP
        FETCH NEXT INTO newLoan;

        IF complete THEN 

       LEAVE sloop;

        END IF;

        SET newLoan = CONCAT (code, isbn, duration);


        CLOSE S;

END$$                
DELIMITER ;

任何想法? 感谢

1 个答案:

答案 0 :(得分:1)

我不太明白你需要做什么,但我看到下面列出的一些问题:

  • 当您声明游标无法使用t1. *时,您必须使用特定列,即:t1.CODE
  • 未声明变量newLoan,必须先声明它才能分配它。
  • LOOP没有结束(END LOOP)。
  • 你得到的变量codeisbnduration

示例:

DELIMITER $$

-- CREATE PROCEDURE `new_entry`(IN `isbn_search` CHAR(17), `useNO` INT)
CREATE PROCEDURE `new_entry`(`isbn_search` CHAR(17), `useNO` INT)
BEGIN
    -- DECLARE `complete` BOOLEAN DEFAULT FALSE;
    DECLARE `complete` BOOL DEFAULT FALSE;
    -- DECLARE `newEntry` VARCHAR (30) DEFAULT ' ';
    DECLARE `newLoan` VARCHAR (30) DEFAULT '';
    DECLARE `CONCAT_newLoan` VARCHAR (60) DEFAULT '';

    /*
    DECLARE S CURSOR FOR
        select t1.*
        from table1 t1
        where typeid = 'mn014' and
          not exists (select 1
                      from table2 t2
                      where t2.code = t.code and
                            t2.rturndte is null
                     );
    */

    /* Simplified statement */
    DECLARE `S` CURSOR FOR
    SELECT `t1`.`CODE`
    FROM `table1` `t1`
    WHERE `TYPEID` = 'mn014';
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET `complete` := TRUE;
    OPEN `S`;

    -- SET newLoan = ' ';

    `sloop`: LOOP
        FETCH `S` INTO `newLoan`;
        IF `complete` THEN
            CLOSE `S`;
            LEAVE `sloop`;
        END IF;
        -- SET `newLoan` = CONCAT(`code`, `isbn`, `duration`);
        /* Simplified statement */
        SET `CONCAT_newLoan` := CONCAT(`CONCAT_newLoan`, `newLoan`);
    END LOOP;
    -- CLOSE S;
    SELECT `CONCAT_newLoan`;
END$$

DELIMITER ;

SQL Fiddle demo