MySQL存储过程中的返回值

时间:2015-07-08 14:49:17

标签: mysql stored-procedures

这是一个非常简单的存储过程,但我无法让它工作。我想根据传入的一些过滤器来计算记录数.where子句完美地工作(它在另一个存储过程中用于返回值)。在这个存储过程中,我只想要基于“where”的TOTAL COUNT。我这样称呼它: 叫countthem('column1','AA','LIKE',@ outfiltercount); 但是,我收到错误消息“错误代码1327:未声明的变量:countit”

CREATE DEFINER=`root`@`%` PROCEDURE countthem(
  IN infilterfield  varchar(25),  
  IN infiltervalue varchar(100), 
  IN infiltertype varchar(10),
  OUT outfiltercount INT)


BEGIN

DECLARE SQLStatement varchar(255);
DECLARE countit INT;


SET @filterfield = infilterfield;
SET @filtervalue = infiltervalue; 
SET @filtertype = infiltertype;

SET outfiltercount = 0;

SET @filterstring = '';

IF infiltertype is not null and infiltertype != ''

  THEN

     IF infiltertype like 'EQUALS'
        THEN
           SET @filterstring = CONCAT(' WHERE ', @filterfield, ' ', ' = ', ' \'', @filtervalue, '\'');
     ELSE
           SET @filterstring = CONCAT(' WHERE ', @filterfield, ' ', ' LIKE ', ' \'%', @filtervalue, '%\'');
     END IF;
END IF;  

SET @SQLStatement = CONCAT('select count(*) into countit from vsltable ', @filterstring);

PREPARE stmt FROM @SQLStatement;

EXECUTE stmt;   

set outfiltercount = countit;

END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

更改

SET @SQLStatement = CONCAT('select count(*) into countit from vsltable ', @filterstring);

SET @SQLStatement = CONCAT('select count(*) as countit from vsltable ', @filterstring);

因此将select ... into替换为select ... as