English 中文(简体)
存储程序多环仅获得第一个循环数据
原标题:Stored Procedure Multiple loops only gets first loop data

我有一个程序有问题 我打电话。

程序有两个循环, 一个连接到同一个网络中的其他数据库, 另一个连接到每个连接的数据, 但是问题在于它只从第一个连接获得第一批数据。 我不确定, 但是也许我在第二个循环中做错了什么, 忘记了某些东西。 这是第二次 I m 进行这样的操作, 第一次对 Ugh 复杂 。

这是程序守则

CREATE PROCEDURE `firians`.`sincronizarAgencias` ()
BEGIN
declare nomeAgencia varchar(255);
declare ultimaAgencia int default false;
declare terminouPicagens int default false;

declare agenciasCur cursor for select ip from agencia;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET ultimaAgencia = TRUE;

open agenciasCur;
nomeAgenciasLoop: LOOP

    FETCH agenciasCur INTO nomeAgencia;

    IF ultimaAgencia THEN
        close agenciasCur; 
        LEAVE nomeAgenciasLoop;
    END IF;

    SELECT nomeAgencia;

    DROP VIEW IF EXISTS temp_agencia_view;
    SET @query = CONCAT( CREATE VIEW temp_agencia_view as select data, idempregado, idsociedade, nif, tipo from ` , nomeAgencia,  ` ); 
    select @query; 
    PREPARE stmt from @query; 
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;

    blocoPicagens: BEGIN
        declare newData DATETIME;
        declare newIdEmpregado VARCHAR(45);
        declare newIdSociedade bigint(20);
        declare newNif varchar(15);
        declare newTipo varchar(45);

        declare ultimaPicagem int default false;

        -- SELECT data, idempregado, idsociedade, nif, tipo from temp_agencia_view;

        DECLARE picagensCursor cursor for select data, idempregado, idsociedade, nif, tipo from temp_agencia_view;
        DECLARE CONTINUE HANDLER FOR NOT FOUND SET ultimaPicagem := TRUE;

        open picagensCursor;
        picagensLoop: LOOP
            fetch picagensCursor into newData, newIdEmpregado, newIdSociedade, newNif, newTipo;

            IF ultimaPicagem THEN
                -- SET ultimaPicagem := false;
                close picagensCursor;
                DROP VIEW IF EXISTS temp_agencia_view; 
                LEAVE picagensLoop;
            END IF;

            INSERT INTO `firians`.`assiduidade`(`data`,`idempregado`,`idsociedade`,`tipo`,`nif`)
            VALUES
            (newData, newIdEmpregado, newIdSociedade, newTipo, newNif);

            INSERT INTO `firians`.`assiduidadebackup`(`data`,`idempregado`,`idsociedade`,`tipo`,`nif`)
            VALUES
            (newData, newIdEmpregado, newIdSociedade, newTipo, newNif);

            set @updateRowQuery = CONCAT( DELETE FROM ` , nomeAgencia, ` WHERE idempregado =  , newIdEmpregado,   AND idsociedade =  , newIdSociedade,   AND tipo =  , newTipo,   AND data LIKE   , newData, " "); 
            select @updateRowQuery;
            PREPARE stmtUpdate from @updateRowQuery; 
            EXECUTE stmtUpdate; 
            DEALLOCATE PREPARE stmtUpdate;

        END LOOP picagensLoop;

    DROP VIEW IF EXISTS temp_agencia_view;
        -- select * from temp_agencia_view;           
    END blocoPicagens;

END LOOP nomeAgenciasLoop;

DROP VIEW IF EXISTS temp_agencia_view;
END

你能帮我吗?

请注意,

埃尔卡斯

最佳回答

我怀疑你的问题是 MySQL 不支持 CREATE VEW 。 您可以使用临时表格代替:

SET @query = CONCAT( 
  CREATE TEMPORARY TABLE temp_agencia_tbl
    select data, idempregado, idsociedade, nif, tipo from ` , nomeAgencia,  `
 );

然而,这一方法似乎太过分了,因为人们可以使用INSERT.SELECT 语法来完全避免第二个光标:

SET @q1 = CONCAT( 
  INSERT INTO firians.assiduidade
          (data, idempregado, idsociedade, tipo, nif)
    SELECT data, idempragado, idsociedade, tipo, nif FROM ` , nomeAgencia,  `
 );
SET @q2 = CONCAT( 
  INSERT INTO firians.assiduidadebackup
          (data, idempregado, idsociedade, tipo, nif)
    SELECT data, idempragado, idsociedade, tipo, nif FROM ` , nomeAgencia,  `
 );
SET @q3 = CONCAT( TRUNCATE ` , nomeAgencia,  ` );

even this 似乎似乎仍然太过分了,因为人们也可以通过使用 GROUP_CONCAT 来构建一个这样的单一的

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...