如何在SQL中的过程中使用select语句

时间:2015-03-25 00:14:54

标签: mysql sql procedure

为什么我的SQL会抛出像LEAVE这样没有匹配标签的错误:tableList

DELIMITER $$
DROP PROCEDURE IF EXISTS CountSignatures$$

CREATE PROCEDURE CountSignatures()
  BEGIN
     DECLARE done INT DEFAULT FALSE;
     DECLARE signatureCount INT;
     DECLARE tableName CHAR(100);
     DECLARE tableList CURSOR FOR Select table_name from information_schema.tables where table_schema="LogData" and table_name like "%FAULT_20150320%";
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
     OPEN tableList;
     tableListLoop: LOOP
       SET done = FALSE ;
       FETCH tableList INTO tableName;
       IF done THEN
         LEAVE tableListLoop;
       END IF;
       ***select signatureCount := signatureCount + count(distinct signature) from tableList;*** Line giving syntax error
     END LOOP;
     CLOSE tableList;
  END$$
DELIMITER;

2 个答案:

答案 0 :(得分:1)

leave lable

  

此语句用于退出具有的流控制结构   给定标签。如果标签用于最外面存储的程序块,   LEAVE退出该计划。

在您的代码中tableList是一个游标,您应该留下tableListLoop而不是tableList,所以请尝试:

LEAVE tableListLoop;

答案 1 :(得分:0)

我不确定为什么它会产生那个特定错误,但这一行不正确:

signatureCount = signatureCount + Select count (distinct signature) from tableList;

如果您想更新变量,请尝试:

set signatureCount = (signatureCount +
                      (select count(distinct signature)
                       from tableList));

但是,你没有初始化变量,所以无论如何这只会产生NULL