Thinking on hiring me?

Please read

Fernando Guillén

a Freelance Web Developer

cabecera decorativa

software development as an artistic expression

Procedimiento almacenado con cursor en MySQL

Aquí dejo el código de un procedimiento almacenado de MySQL que usa un cursor.

Esto no es un tutorial es una nota mental.

DROP procedure IF EXISTS test_cursor
/
 
CREATE procedure test_cursor()
 
begin
 
--
-- variables
--
declare hasMoreRows bool DEFAULT true;
declare _field1 varchar(20);
declare _field2 varchar(20);
 
--
-- the cursor
--
declare cur cursor FOR
 
  SELECT
      cl.LIBRARY_NAME,
      cl.LIBRARY_DESCRIPTION
  FROM
      CAT_LIBRARIES AS cl
  ;
 
declare continue handler FOR SQLSTATE '02000'
    SET hasMoreRows = false;
 
--
-- create table test
--
DROP TABLE IF EXISTS test_cursor;
 
CREATE TABLE `test_cursor` (
  `field1`  varchar(20) NULL,
  `field2`  varchar(20) NULL
);
 
--
-- open the cursor
--
open cur;
 
fetch cur INTO
  _field1,
  _field2;
 
while hasMoreRows do
 
--
-- ####### WALK:INI ########
--
 
  INSERT INTO
      test_cursor
  SET
    field1 = _field1,
    field2 = _field2
  ;
 
--
-- ####### WALK:END ########
--
 
  fetch cur INTO
    _field1,
    _field2;
 
end while;
 
close cur;
 
end;
/

Aquí está el fichero con el código fuente.

2 Comments to “Procedimiento almacenado con cursor en MySQL”
  1. fguillen Says:

    Nota: cuidado con los dobles guiones que WordPress los mutila y los convierte en un caracter raro de guión largo.

  2. Rebeca Says:

    Este si me funciono, recorrer un select !!! buena

Leave a comment

You must be logged in to post a comment.

a Freelance Web Developer is proudly powered by WordPress
Entries (RSS) and Comments (RSS).

Creative Commons License
Fernando Guillen's blog by Fernando Guillen is licensed under a Creative Commons Attribution-NoDerivs 3.0 Unported License.