call spQuery_Lista_Producto_Linea('P', NULL, NULL);

DROP PROCEDURE IF EXISTS spQuery_Lista_Producto_Linea;
CREATE PROCEDURE spQuery_Lista_Producto_Linea(
  varTip  char(1),
  varCod  varchar(2),
  varNom  varchar(45)
  )
BEGIN
  DECLARE varCadCod, varCadNom TEXT DEFAULT '';
  
	if varCod <> '' and varCod is not null then
    set varCadCod = CONCAT(" and id_tipo = '", lpad(varCod,2,'0'),"' ");
  end if;
	if varNom <> '' and varNom is not null then
    set varCadNom = CONCAT(" and nombre like '%", varNom,"%' ");
  end if;

 	SET @sql = CONCAT("select id_tipo, nombre ",
                    "from _sv_tipo ",
                    "where _estado='A' ",
                    "and flag_proser = '",varTip,"' ",
                    varCadCod, varCadNom,
                    "order by nombre");
  PREPARE s1 FROM @sql;
  EXECUTE s1;
  DEALLOCATE PREPARE s1;
END;