【问题标题】:MySQL Returning Value in Stored Proc存储过程中的 MySQL 返回值
【发布时间】:2015-07-09 21:48:16
【问题描述】:

这是一个非常简单的存储过程,但我无法让它工作。我想根据传入的一些过滤器来计算记录的数量。 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 ;

【问题讨论】:

  • 永远不会分配您的 countit 变量。
  • 我不确定你说的从未分配过是什么意思?我尝试在它进入“IF”之前将其设置为 0....但是,它仍然不起作用。

标签: mysql stored-procedures


【解决方案1】:

改变

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    • 2011-10-15
    • 2013-02-02
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多