循环外不起作用

时间:2018-08-16 19:35:07

标签: mysql loops procedure

如何在另一个内部循环?…….....................

但是外部循环不起作用。仅内部循环运行.......

我有这些表:........................

人:

 ________________
| code | name    |
|   1  | Carlos  |
|   2  | Charles |
|   3  | Mary    |
|   4  | Jhon    |
|   5  | Jen     |
-----------------

课程:

  ________________
| code | desc       |
|   1  | Java       |
|   2  | PHP        |
|   3  | Perl       |
|   4  | Python     |
|   5  | Javascript |
-------------------

这些是我程序的一部分:

DECLARE v_cdclass INT;
DELCARE v_codperson INT;
DROP TABLE IF EXISTS temp1;
CREATE TEMPORARY TABLE temp1 
(
  id  INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,  
  cdfunc         INT(11),
  class           INT
); 
OPEN R; 
    loop0: LOOP
         FETCH R INTO v_codperson; 
            IF done2 THEN
               LEAVE loop0;
            END IF;

            BLOCO2: BEGIN

              OPEN classes;      
                 loop1: LOOP    
                    FETCH classes INTO v_cdclass ;  
                    IF done1 THEN
                        LEAVE loop1;
                    END IF;
                        INSERT INTO temp1 
                        ( cdfunc,  class )
                        VALUES 
                        ( v_codperson , v_cdclass);  
                  END LOOP loop1;   
                CLOSE classes;        

            END BLOCO2;
    END LOOP ;      
CLOSE R; 
SELECT * FROM temp1; 

1 个答案:

答案 0 :(得分:1)

尝试:

DELIMITER \\

DROP PROCEDURE IF EXISTS `sp_test`\\
DROP TABLE IF EXISTS `classes`, `people`\\

CREATE TABLE IF NOT EXISTS `people` (
  `code` SERIAL,
  `name` VARCHAR(25)
)\\

CREATE TABLE IF NOT EXISTS `classes` (
  `code` SERIAL,
  `desc` VARCHAR(25)
)\\

INSERT INTO `people`
  (`name`)
VALUES
  ('Carlos'), ('Charles'),
  ('Mary'), ('Jhon'),
  ('Jen')\\

INSERT INTO `classes`
  (`desc`)
VALUES
  ('Java'), ('PHP'),
  ('Perl'), ('Python'),
  ('Javascript')\\

CREATE PROCEDURE `sp_test`()
BEGIN
  DECLARE `v_cdclass` BIGINT UNSIGNED;
  DECLARE `v_codperson` BIGINT UNSIGNED;
  DECLARE `done2` BOOL;
  DECLARE `R` CURSOR FOR
    SELECT `code` FROM `people`;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done2` := TRUE;
  DROP TEMPORARY TABLE IF EXISTS `temp1`;
  CREATE TEMPORARY TABLE IF NOT EXISTS `temp1` (
    `id`  SERIAL,
    `cdfunc` BIGINT UNSIGNED,
    `class` BIGINT UNSIGNED
  ); 
  OPEN `R`;
    `loop0`: LOOP
      FETCH `R` INTO `v_codperson`;
      IF `done2` THEN
        LEAVE `loop0`;
      END IF;
      `BLOCO2`: BEGIN
        DECLARE `done1` BOOL;
        DECLARE `classes` CURSOR FOR
          SELECT DISTINCT `code` FROM `classes`;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET `done1` := TRUE;
        OPEN `classes`;
        `loop1`: LOOP
          FETCH `classes` INTO `v_cdclass`;
          IF `done1` THEN
            LEAVE `loop1`;
          END IF;
          INSERT INTO `temp1`
            (`cdfunc`, `class`)
          VALUES 
            (`v_codperson` , `v_cdclass`);
        END LOOP `loop1`;   
        CLOSE `classes`;        
      END `BLOCO2`;
    END LOOP `loop0`;
  CLOSE R; 
  SELECT `cdfunc`, `class`
  FROM `temp1`;
END\\

CALL `sp_test`\\

DROP PROCEDURE IF EXISTS `sp_test`\\
DROP TEMPORARY TABLE IF EXISTS `temp1`\\
DROP TABLE IF EXISTS `classes`, `people`\\

DELIMITER ;

请参见Rextester