mysql存储过程使用ROW_COUNT()作为条件

时间:2017-11-04 18:26:21

标签: mysql stored-procedures

在存储过程中我有这样的一些:

SET @input_var1 = 'some1';
SET @input_var2 = 'some2';

SET @query = "SELECT * FROM my_table WHERE my_column = ?";

PREPARE stmt FROM @query;
EXECUTE stmt USING @input_var1;
SET @row_count = ROW_COUNT();
DEALLOCATE PREPARE stmt;


IF(@row_count = 0)THEN
    SET @query = "SELECT * FROM my_table WHERE my_column = ?";

    PREPARE stmt FROM @query;
    EXECUTE stmt USING @input_var2;
    SET @row_count = ROW_COUNT();
    DEALLOCATE PREPARE stmt;    
END IF;

问题:

@row_count始终返回-1

Espected:

  • @row_count = 0 ...如果为空结果
  • @row_count = 1 ...如果结果是一行
  • @row_count> 0 ...如果结果不为空(一行或多行)

0 个答案:

没有答案