使通配符在变量MySQL存储过程中工作

时间:2015-01-30 16:27:38

标签: mysql stored-procedures

我编写了一个存储过程,它使用带有sql查询的变量使其动态,具体取决于一组参数,我的问题是当我尝试使用%通配符的LIKE子句时,查询不是过滤,我测试了一个常规查询,它的工作原理。

CREATE DEFINER=`root`@`localhost` PROCEDURE `seleccionar_exp_repGral`(in p_fecha date, in p_fecha2 date, in p_compania int, in p_institucion int, in p_sector int, 
in p_clasificacion int, in p_analista int, in p_decision int, in p_fuenteFinanciamiento int, in p_tipo int, in p_objeto varchar(3500))
BEGIN
   set @qrystr = "select Expe.exp, Expe.mes, Expe.anio, Expe.fecha_recibido, cia.nombre_com, inst.nombre_inst, Expe.monto_egreso,
   Expe.anio_egreso, Expe.monto_ingreso, Expe.anio_ingreso, an.nombre_ana, deci.nombre_dec, Expe.fecha_sesion, sec.nombre_sec, 
   clas.nombre_cla, finan.nombre_fin, tipo.nombre_tipo from expediente Expe
   left outer join compania cia on (Expe.compania = cia.idcompania)
   left outer join institucion inst on (Expe.institucion = inst.idinstitucion)
   left outer join analista an on (Expe.analista = an.idanalista)
   left outer join decision deci on (Expe.decision = deci.iddecision)
   left outer join sector sec on (Expe.sector = idsector)
   left outer join clasificacion clas on (Expe.clasificacion = idclasificacion)
   left outer join financiamiento finan on (Expe.financiamiento = finan.idfinanciamiento)
   left outer join tipo_expediente tipo on (Expe.tipo = tipo.idtipo_expediente)";

-- variables for filter
set @pcia = p_compania;
set @pinst = p_institucion;
set @psector = p_sector;
set @pclasificacion = p_clasificacion;
set @panalista = p_analista;
set @pdecision = p_decision;
set @pfuenteFinanciamiento = p_fuenteFinanciamiento;
set @ptipo = p_tipo;
set @pObjeto = p_objeto;

-- concatenate conditions for filter
set @qrystr = CONCAT(@qrystr,'where Expe.fecha_recibido >=\'',p_fecha,'\' and Expe.fecha_recibido <=\'',p_fecha2,
'\'');
-- Filters
IF @pcia > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.compania = ',@pcia);
end if;
IF @pinst > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.institucion = ',@pinst);
end if;
IF @psector > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.sector = ',@psector);
end if;
IF @pclasificacion > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.clasificacion = ',@pclasificacion);
end if;
IF @panalista > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.analista = ',@panalista);
end if;
IF @pdecision > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.decision = ',@pdecision);
end if;
IF @pfuenteFinanciamiento > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.financiamiento = ',@pfuenteFinanciamiento);
end if;
IF @ptipo > 0 THEN
    set @qrystr = CONCAT(@qrystr,' and Expe.tipo = ',@ptipo);
end if;
IF @pObjeto <> "" THEN
    set @qrtystr = CONCAT(@qrystr," and Expe.objeto LIKE '",@pObjeto,"'");
end if;
-- preparamos el ultimo statement del query
set @qrystr = CONCAT(@qrystr,' order by Expe.idexpediente asc');
-- preparamos datos finales
PREPARE stmt from @qrystr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$

从那个漫长的SP我有这个部分:

    IF @pObjeto <> "" THEN
     set @qrtystr = CONCAT(@qrystr," and Expe.objeto LIKE '",@pObjeto,"'");
    end if;

这一行我在@pObjeto之前添加了通配符%,之后它不起作用,我甚至在变量中添加了通配符,我的查询仍显示没有文本过滤的所有寄存器。

我想知道它是否与字符串格式化有关。

我感谢任何帮助。

0 个答案:

没有答案