0

我创建了一个带有游标的存储过程,以便从几个表中获取信息到一个临时表中,然后将结果导出到多个文件中,每个文件对应一个客户端 ID,游标和循环获取所有信息,但我m 在创建每个文件时遇到语法问题

BEGIN
    DECLARE id_cli INT;
  DECLARE cur_id_cli CURSOR FOR SELECT id FROM cliente ORDER BY id;

  OPEN cur_id_cli;

  read_loop: LOOP
    FETCH cur_id_cli INTO id_cli;


DROP TEMPORARY TABLE IF EXISTS tmp_reporte_enviadas_sucursal_mensual;
  CREATE TEMPORARY TABLE tmp_reporte_enviadas_sucursal_mensual

  SELECT  *  FROM
  ((SELECT 'Promo_id', 'Mensaje', 'Sucursal_id', 'Direccion') UNION ALL (SELECT
      p.id,
          p.mensaje,
          s.id,
          s.direccion
  FROM
      Usuario_Promo AS up, Promo_Sucursal AS ps, Cliente_Sucursal AS cs, Cliente AS c, Promo AS p, Sucursal AS s
  WHERE
      p.id = ps.Promo_id
          AND up.promo_id = ps.id
          AND up.recibido = 1
          AND ps.Cliente_Sucursal_id = cs.id
          AND cs.cliente_id = id_cli
          AND DATE(up.fecha_recibido) BETWEEN (NOW() - INTERVAL 30 DAY) AND NOW()
          AND s.id = cs.sucursal_id
  ORDER BY p.id)) AS tmp_reporte_enviadas_sucursal_mensual;

SET @idCli = CAST(id_cli AS CHAR);
SET @fullOutputPath = CONCAT('D:/Octagon/Apps/Flyermob/Clientes/Reportes_Sql/reporte_enviadas_sucursal_mensual_',@idCli,'.csv');

SELECT * FROM
INTO OUTFILE @fullOutputPath 
FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '\"'
FROM tmp_reporte_enviadas_sucursal_mensual;

SELECT * FROM tmp_reporte_enviadas_sucursal_mensual;

  END LOOP;

  CLOSE cur_id_cli;
END

这是我遇到问题的地方:

SELECT * FROM
    INTO OUTFILE @fullOutputPath 
    FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '\"'
    FROM tmp_reporte_enviadas_sucursal_mensual;

每当我尝试保存时,如果我删除此部分,它就会说附近存在语法问题@fullOutputPath,该过程运行完美,并且我获得了每个客户端 ID 的所有结果。

只是为了摆脱它id_cli是一个主键,所以没有重复的机会,我对@fullOutputPath.

4

2 回答 2

0

尝试这个:

SELECT * FROM tmp_reporte_enviadas_sucursal_mensual
    INTO OUTFILE @fullOutputPath 
    FIELDS TERMINATED BY ',' 
    OPTIONALLY ENCLOSED BY '\"';
于 2017-04-14T17:47:57.953 回答
0

我设法使它与

SET @idCli = CAST(id_cli AS CHAR);
SET @fullOutputPath = CONCAT('flyermob/cliente/reporte_enviadas_sucursal_mensual_cliente_',@idCli,'.csv');

set @q1 := concat("SELECT * INTO OUTFILE '",@fullOutputPath,
"' FIELDS TERMINATED BY ',' 
OPTIONALLY ENCLOSED BY '\"'
FROM tmp_reporte_enviadas_sucursal_mensual");

prepare s1 from @q1;
execute s1;
deallocate prepare s1;

它在 MySql 默认文件夹中创建文件

于 2017-05-04T20:30:23.970 回答