我目前正致力于自动化将MySQL数据导出到制表符分隔文件的流程,并动态确定需要输出的字段列表。我已经克服了许多障碍,但现在我甚至在搜索了SO,谷歌等之后仍然坚持了一些事情。
具体来说,我有一个存储过程存在,并且从mysql提示符运行时完全按预期工作;但是,当我尝试从shell脚本调用相同的存储过程时,我收到错误PROCEDURE schemaName.procedureName does not exist.
供参考,这是shell脚本:
#!/bin/bash
if [ $# -lt 4 ]; then
echo $'\nPlease include the following arguments:'
echo $'\t- source schema name (aka, database name in MySQL)'
echo $'\t- source data table name'
echo $'\t- source database environment (prod, qa, or dev)'
echo $'\t- database username\n'
echo $'The arguments must be entered in that order.\n'
echo $'Do not include the path for the destination filename. It will be saved to ~/csv-files/<table name>.'
exit
fi
[[ -d ~/csv-files ]] || mkdir ~/csv-files
cols=$(echo "call $1.tab_delim_col_names('$2',@colNames)" | source ~/bin/$3_db_login $4)
echo "select concat($cols) from $1.$2" | source ~/bin/$3_db_login $4 > ~/csv-files/$1.$2.csv
并且,这是存储过程:
delimiter //
create procedure tab_delim_col_names(
IN tableName VARCHAR(100),
OUT columnList VARCHAR(1000)
)
begin
declare n int default 0;
declare i int default 0;
declare colNames varchar(1000);
select count(*) into @getCount
from information_schema.columns
where table_name = tableName;
set n = @getCount;
set i = 0;
while i < n do
select column_name into @thisCol
from information_schema.columns
where table_name = tableName
limit i,1;
if i > 0 then
set colNames = concat(colNames, ',\'\\t\',', @thisCol);
else
set colNames = @thisCol;
end if;
set i = i + 1;
end while;
select colNames;
end;//
delimiter ;
如上所述,当我从mysql提示符执行此操作时,一切都运行良好(没有选择数据库):
call services.tab_delim_col_names('tableName',@colNames);
但是,当bash脚本执行此行时,我得到“PROCEDURE不存在”错误:
cols=$(echo "call $1.tab_delim_col_names('$2',@colNames)" | source ~/bin/$3_db_login $4)
我已经尝试过编写1美元和2美元的硬编码但它仍然无法正常工作,所以传递的问题也不错。
非常感谢任何帮助!
P.S。 - 我还担心程序结束时的select colNames
不会在$ cols中存储一个可以成功用作concat()参数的值,但是在这个问题出现之前我无法确定解决。