2017-04-14 91 views
0

我創建了一個帶有遊標的存儲過程,以便從幾張表中獲取信息到一個臨時表中,然後將結果導出到多個文件中,每個文件對應一個客戶端ID,遊標和循環得到的所有信息的權利,但在創建每個文件MySQL導出到Outfile問題

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附近如果我刪除這部分,程序運行perf實際上,我得到每個客戶端ID的所有結果。

只是爲了避免這個問題id_cli是一個主鍵,所以沒有重複的機會,我擁有@fullOutputPath的完整權限。

+0

'''SELECT * FROM ???' ''需要有一個表名,其中的「???」是。 –

回答

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默認文件夾

0

試試這個:

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

嗨,感謝您的回覆,我試過了,仍然在'@ fullOutputPath'上出現了語法錯誤 – gusadolfo