MySQL 导出到 Outfile 问题

MySQL export into Outfile issue

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

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.

拥有完全权限

试试这个:

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

我设法让它与

一起工作
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 默认文件夹中创建文件