参数sql查询存储过程mysql里面

时间:2013-12-24 03:23:25

标签: mysql sql stored-procedures

我正在使用mysql中的存储过程,所以我有以下过程:

DELIMITER @@
DROP PROCEDURE IF EXISTS generarEstadisticoRD @@
CREATE PROCEDURE generarEstadisticoRD ( mesInicial INT,anualInicial INT, mesFinal INT,anualFinal INT, codigoEntidad CHAR(3),mes INT )

BEGIN
    DECLARE controlador INT;
    DECLARE tipoDocumento CHAR(2);

    DECLARE cursorDocumentos CURSOR FOR SELECT DISTINCT e.claseDocIdentidadFallecido 
            FROM EstadisticoRD e  WHERE e.anual>=anualInicial AND e.anual<=anualFinal  
            AND  e.mes >=mesInicial AND e.mes<=mesFinal AND e.codOficina=codigoEntidad;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET controlador = 1;

    DROP TEMPORARY TABLE IF EXISTS estadistico;
    CREATE TEMPORARY TABLE IF NOT EXISTS 
    estadistico( TIPO CHAR(2), MES INT );

    OPEN cursorDocumentos;

    cursorLoop : LOOP
        FETCH cursorDocumentos INTO tipoDocumento;
        IF( controlador=1 ) THEN
            LEAVE cursorLoop;
        END IF 

        /**
          *Lógica
          */
        INSERT INTO estadistico(`TIPO`,`MES`) 
            SELECT DISTINCT 
                c.descripcion, 
                IFNULL( (SELECT e.numRegistrosReportados FROM estadisticoRD e WHERE e.codOficina=codigoEntidad
                    AND e.claseDocIdentidadFallecido=tipoDocumento AND e.mes=mes ), 0)
            FROM estadisticoRD e, claseDoc c WHERE e.codOficina=codigoEntidad AND e.claseDocIdentidadFallecido=tipoDocumento
            AND c.claseDoc = e.claseDocIdentidadFallecido;

    END LOOP cursorLoop;
    CLOSE cursorDocumentos;
    SELECT * FROM estadistico;
END @@ 
DELIMITER ;

当我尝试执行该过程时,我收到以下消息:

Executed successfully in 0,001 s, 0 rows affected.
Line 2, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO estadistico(`TIPO`,`MES`) 
        SELECT DISTINCT c.descripcion, 
 ' at line 24
Line 3, column 1

那么,我做错了什么?。

更新1:

我用分号纠正了错误,感谢@Daniel Victoria

但现在我得到以下错误:

Error code 1267, SQL state HY000: Illegal mix of collations (latin1_spanish_ci,IMPLICIT) and (latin1_swedish_ci,IMPLICIT) for operation '='

当我做

时,我确实犯了这个错误
SELECT DISTINCT e.claseDocIdentidadFallecido 
            FROM EstadisticoRD e  WHERE ...  AND e.codOficina=codigoEntidad;

为什么当我e.codOficina=codigoEntidad我犯了这个错误,如何解决它?。

更新2:

要解决这个问题,我需要将COLLATE latin1_swedish_ci放到有错误的列之后。

在这种情况下,新查询是:

SELECT DISTINCT * 
            FROM estadisticoRD e  WHERE e.anual>=anualInicial AND e.anual<=anualFinal  
            AND  e.mes >=mesInicial AND e.mes<=mesFinal AND e.codOficina  = codigoEntidad COLLATE latin1_swedish_ci;

我希望以最好的方式完成这个程序。

1 个答案:

答案 0 :(得分:1)

您在“END IF”之后缺少分号(;)