【问题标题】:Stored Procedure not returning multiple rows using Cursor in mysql存储过程不使用 mysql 中的游标返回多行
【发布时间】:2016-03-28 13:41:23
【问题描述】:

这是我见过的示例程序Blog written by Kevin Bedell 我无法从 mysql 中的以下程序中获取所有记录。当我调用此过程时,它返回单行 -

存储过程 -

DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `usp_cursor_example`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `usp_cursor_example`(
   IN name_in VARCHAR(255)
)
READS SQL DATA
BEGIN
  DECLARE name_val VARCHAR(255);
  DECLARE status_update_val VARCHAR(255);
  DECLARE no_more_rows BOOLEAN;
  DECLARE loop_cntr INT DEFAULT 0;
  DECLARE num_rows INT DEFAULT 0;
  DECLARE friends_cur CURSOR FOR
     SELECT
        `name`
     , status_update
     FROM test.friend_status
     WHERE `name` = name_in;
  DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
OPEN friends_cur;
 SELECT FOUND_ROWS() INTO num_rows;
  the_loop: LOOP
  FETCH  friends_cur
 INTO   name_val
,      status_update_val;
IF no_more_rows THEN
    CLOSE friends_cur;
    LEAVE the_loop;
END IF;
SET loop_cntr = loop_cntr + 1;
END LOOP the_loop;
SELECT name_val, status_update_val;
-- SELECT num_rows, loop_cntr;
END$$
DELIMITER ;

运行使用 -

call usp_cursor_example('John');

它应该返回 4 行,但它只返回 4 行中的第一行。

表结构 -

drop table if exists test.friend_status; 

CREATE TABLE IF NOT EXISTS `test`.`friend_status` (
    `id`            INTEGER(10) unsigned NOT NULL auto_increment,
    `name`          VARCHAR(255) NOT NULL,
    `status_update` VARCHAR(255) NOT NULL,
    PRIMARY KEY (`id`)
);

insert into test.friend_status
    (name, status_update)
  values
      ('John',  'Woke up. Guiness for Brkfst.')
    , ('Fred',  'is thinking about joining the circus')
    , ('Erin',  "Getting ready for a job interview")
    , ('Amy',   'at work and dreaming of kittens')
    , ('John',  'Watching Scooby Doo reruns. Guiness for Lunch.')
    , ('Amy',   'dreaming of fuzzy slippers and wedding dresses')
    , ('Julie', 'is hating working two jobs')
    , ('John',  'Out of the shower finally. Guiness for Dinner.')
    , ('Erin',  "if I don't get this job, I'll be asking 'Paper or Plastic?'")
    , ('Amy',   'dreaming of Meeting Mr. Right!')
    , ('Erin',  'Nailed the job interview -- calling John to celebrate!')
    , ('Amy',   'John called -- meeting him at the pub!')
    , ('John',  'Heading out to meet friends for some Guiness!')
;

我在堆栈溢出中没有看到任何答案,但使用这些答案我无法获取所有记录。请建议我从存储过程中获取这些记录的方法是什么?

【问题讨论】:

  • 您的存储过程似乎正在完成这项工作。 SELECT name_val, status_update_val; 应该返回一条记录并且应该使用 NULL 值。见SQL Fiddle demo
  • @wchiquito - 感谢您的建议,但我想查看 'John' 的四条记录,而不是记录的数量。我从我的问题中删除了另一个选择语句。
  • 之前的demo显示游标运行正常,现在的问题是:是否需要游标才能看到4条记录?通过一个简单的查询,它就得到了它需要的东西。见SQL Fiddle demo
  • @wchiquito - 我非常感谢您的努力,但我只想使用游标获取所有记录,因为我想使用游标对字段值进行一些操作,然后显示所有记录。我已经向您展示了存储过程的示例,但实际上在从游标中获取后正在执行更多任务。
  • 一种选择是使用临时表。见SQL Fiddle demo

标签: mysql stored-procedures cursor


【解决方案1】:
SELECT name_val, status_update_val;

将上述行放在循环中以获取所有记录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-24
    • 2016-10-31
    • 2013-12-19
    相关资源
    最近更新 更多