Mysql使用游标存储过程

时间:2014-01-01 04:42:30

标签: mysql stored-procedures cursor

Mysql游标问题?

我编写了一个存储过程,它将从一个表中传输记录并将其插入2-3个不同的表中 使用插入语句。

问题是我正在检查table1中是否存在记录然后我将temptable中的记录插入到table1,table2中 ,但条件是有一些问题我不知道它总是进入其他部分。

代码示例如下:

CREATE PROCEDURE `insertData`(In clientNo INT,In usedID INT)
BEGIN

declare mame varchar(100);
declare address varchar(100);
declare city varchar(50);
declare IdentityNO1 varchar(20)


declare cur1 cursor for select * from temptable;
declare continue handler for not found set done=1;

SET @clientNo = clientNO;
SET @userID = userID;


    set done = 0;
    open cur1;
    igmLoop: loop
        fetch cur1 into Name,Address,City,IdentityNO1,clientNo;
        if done = 1 then leave igmLoop; end if;

        //If no record exists in some records table1,table2.
        IF ( (SELECT COUNT(*) FROM table1 
               WHERE IndentityNo=IdentityNo1 
                 AND clientNo=@clientNo) < = 0)
              INSERT INTO table1 (Name,IdentityNO) VALUES (name,IdentityNO1);
              INSERT INTO table2 (Address,City) VALUES(address,city);
        ELSE
              INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
              VALUES(name,address,city,IdentityNo1);
    end loop igmLoop;
    close cur1;
END

1 个答案:

答案 0 :(得分:17)

没有THENEND IF关键字,程序无法编译 请查看此链接,了解IF语句的正确语法:http://dev.mysql.com/doc/refman/5.7/en/if.html

使用EXIST运算符代替(SELECT count(*)... ) <=0
阅读此链接以了解原因:http://sqlblog.com/blogs/andrew_kelly/archive/2007/12/15/exists-vs-count-the-battle-never-ends.aspx

IF    EXISTS(  
               SELECT null FROM table1 
               WHERE IndentityNo=IdentityNo1 
                 AND clientNo=@clientNo
      )
THEN
              INSERT INTO table1 (Name,IdentityNO) VALUES (name,IdentityNO1);
              INSERT INTO table2 (Address,City) VALUES(address,city);
ELSE
              INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
              VALUES(name,address,city,IdentityNo1);
END IF;


我建议为过程参数和变量名使用一些前缀以避免歧义,例如对参数使用p_,对变量使用v_。看这段代码很难猜测,哪个名称是列名,变量或过程参数。这可能导致错误和错误。

避免使用SELECT * - 如果有人更改表结构,此代码将失败。显式列出游标声明中的必需列:

declare cur1 cursor for 
        select name,Address,City,IdentityNO,clientNo
        from temptable;

更正的程序可能如下所示:

CREATE PROCEDURE `insertData`(In p_clientNo INT,In p_usedID INT)
BEGIN

declare v_name varchar(100);
declare v_address varchar(100);
declare v_city varchar(50);
declare v_IdentityNO varchar(20)
declare v_clientNo int


declare cur1 cursor for 
        select name,Address,City,IdentityNO,clientNo
        from temptable;
declare continue handler for not found set done=1;

    set done = 0;
    open cur1;
    igmLoop: loop
        fetch cur1 into v_name,v_Address,v_City,v_IdentityNO,v_clientNo;
        if done = 1 then leave igmLoop; end if;

        //If no record exists in some records table1,table2.
        IF  EXISTS(  SELECT 1 FROM table1 
                     WHERE IndentityNo = v_IdentityNo 
                       AND clientNo = v_clientNo)
              INSERT INTO table1 (Name,IdentityNO) VALUES (v_name,v_IdentityNO);
              INSERT INTO table2 (Address,City) VALUES(v_address,v_city);
        ELSE
              INSERT INTO tblexceptional(Name,Address,City,IdentityNo)
              VALUES(v_name,v_address,v_city,v_IdentityNo);
        END IF;
    end loop igmLoop;
    close cur1;
END