问题
I am currently building a MySQL database. The database is storing data which is being supplied by another organisation and the format of the files we recieve is, shall we say, somewhat inconsistent.
I have writted a function which assess the file based on some simple rules and returns a message saying 'file ok' or else one of a list of hard coded error messages. The way I would like to use this function would be to say something like:
if check_10m_file() = 'file ok' then
load data infile '\\\\server\\filepath\file.csv'
fields terminated by ','
...(etc)
My problem is that the if..then..else
control structure does not seem to be allowed outside of stored procedures and the load data infile
command is not allowed inside stored procedures.
I have attempted to trick my way around this by building the load data infile
statement as a prepared statement but then I get an error
"This command is not supported in the prepared statement protocol yet"
So my question is, does anyone know of a way in which I can run a load data statement only when conditions are met? I would ideally like to put this into a stored procedure but if I could just save the code as a script to run later, that would be acceptable.
回答1:
One option is to use UDF, for example: lib_mysqludf_sys.
After installing the UDF, you can do something like:
Shell script (/server/loadpath/load.sh):
mysql -u [user] -p[pass] -e "LOAD DATA INFILE '$1' INTO TABLE $2;"
Stored Procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS load_data$$
CREATE PROCEDURE load_data(pfile VARCHAR(100), pdbtable VARCHAR(100))
BEGIN
DECLARE exec_str VARCHAR(500);
DECLARE ret_val INT;
IF (check_10m_file() = 'file ok') THEN
SET exec_str := CONCAT('sh /server/loadpath/load.sh ', pfile, ' ', pdbtable);
SET ret_val := sys_exec(exec_str);
IF ret_val = 0 THEN
SELECT 'OK' Result;
ELSE
SELECT 'ERROR' Result;
END IF;
END IF;
END$$
DELIMITER ;
CALL load_data('/server/filepath/file.csv', 'mydb.mytable');
IMPORTANT: Validate the input data to prevent any code injection.
来源:https://stackoverflow.com/questions/19053263/mysql-load-data-infile-when-conditions-are-met