ErrCode with "Select Into Outfile with a variable" - Confusing Permissions

142 Views Asked by At

Background: I work with phpMyAdmin (MySQL Workbench) in a mysql DB. I write some PHP code to import data in the DB and execute this with the task scheduler of windows. <= this works fine!

Now I want to export some data into a file in a Windows folder. At first I write the SQL code in phpMyAdmin to see some debug-infos. <= this is where the problem occurs.

My Topic: I want to export some columns of my DB. My Goal is to put a variable CURRENT_TIMESTAMP in the filename. For this I use the Concat statement. My code (posted below), gets the result of the following error:

Can't create/write to file 'C:\Temp\Export\2018-08-08 09:21:27.txt' (Errcode: 13 "Permission denied")

Funny thing is, if I replace the variable CURRENT_TIMESTAMP with e.g. "Hello World" there is no error and my file is created in the folder.

Here is my code:

*set @sql = concat("SELECT `LS_ID_Nr`,
        `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL`, 
        `Umstellzeit`, 
        `Produktionszeit`, 
        `Teilmeldung`, 
        `Fertigmeldung` INTO OUTFILE 'C:/Temp/Export/",CURRENT_TIMESTAMP,".txt' fields terminated by ';' lines terminated by '\r\n' From praemie where Proof_P = 0");
prepare s1 from @sql;
execute s1;
DROP PREPARE s1;
UPDATE praemie SET Proof_P = 1 WHERE Proof_P = 0;*

Does anybody have an idea why there is an Permission Error with the use of a variable? Thanks in advance!

1

There are 1 best solutions below

0
On

Ooooohh....

I get it now! The problem is, that windows not handle ":" in filenames. So I have to edit the code with the Date_Format statement like this:

set @sql = 
concat("SELECT `LS_ID_Nr`, `Stk_pro_Krt_DL` * `Krt_DL` + `RB_Stk_pro_Krt_DL` * `RB_Krt_DL`, 
`Umstellzeit`, `Produktionszeit`, `Teilmeldung`, `Fertigmeldung` 
INTO OUTFILE 'C:/Temp/Export/Test - ", DATE_FORMAT(NOW(), '%Y%m%d%H%i%s')," - Test.txt'
 fields terminated by ';' 
lines terminated by '\r\n' 
From praemie where Proof_P = 0")