我使用游标创建了一个存储过程,以便将几个表中的信息转换为临时表,然后将结果导出到多个文件中,每个客户端ID一个,游标和循环获取所有信息权限但我在创建每个文件时遇到语法问题
simple_session = tf.Session()
value_of_c = simple_session.run(c)
print(value_of_c) # 17
simple_session.close()
这里是我遇到问题的地方:
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;
附近存在语法问题,如果我删除此部分,则该过程运行良好,我会获得每个客户端ID的所有结果。
只是为了让它不受@fullOutputPath
主键影响,所以没有机会重复,我对id_cli
拥有完全权限。
答案 0 :(得分:0)
试试这个:
{{1}}
答案 1 :(得分: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默认文件夹
中创建文件