我的批处理文件将包含以下内容:
set MUSER=root
set MPASS=xprd123
set MHOST=172.33.49.55
set DB=DBName1
set MBIN=C:\Program Files\MySQL\MySQL Server 5.5\bin
set scriptpath=D:/Graph_draw/ASSET_Creation/EXTERNAL/MYSQL_STATS_COLLECTOR/foldrname1
"%MBIN%\mysql.exe" -u"%MUSER%" -p"%MPASS%" -h "%MHOST%" "%DB%" -e "set @2:='%scriptpath%'; source DBQuery1.sql;"
DBQuery1.sql的内容将是:
SELECT 'INNODB_OS_LOG_WRITTEN','QUESTIONS'
UNION
select MAX(IF(variable_name = 'INNODB_OS_LOG_WRITTEN', VARIABLE_VALUE, NULL))
AS INNODB_OS_LOG_WRITTEN, MAX(IF(variable_name = 'QUESTIONS', VARIABLE_VALUE, NULL))
AS QUESTIONS from information_schema.GLOBAL_STATUS INTO OUTFILE '@2/Stats.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
我不确定这是将路径传递给DBQuery1.sql中的SELECT查询的方法。
此代码无效。
我得到的错误是:
ERROR 1 (HY000) at line 1 in file: 'DBQuery1.sql': Can't create/write to file 'C
:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.5\Data\
@2\Stats.csv' (Errcode: 2)
我查看了批量文件传递路径上的其他帖子,但无论我尝试过什么,我都无法解决这个问题。
当我执行批处理文件时,我不确定路径是否传递到sql文件中,如果是,为什么@ 2没有传递路径。
有人可以帮忙吗。
更新:
为了添加上面给出的细节,我发现传递的路径是在sql文件中接收的。
我打印了这样的变量输出:
**SELECT @2;**
SELECT 'INNODB_OS_LOG_WRITTEN','QUESTIONS'
UNION
select MAX(IF(variable_name = 'INNODB_OS_LOG_WRITTEN', VARIABLE_VALUE, NULL)) AS INNODB_OS_LOG_WRITTEN,
MAX(IF(variable_name = 'QUESTIONS', VARIABLE_VALUE, NULL)) AS QUESTIONS
from information_schema.GLOBAL_STATUS
INTO OUTFILE '@2\Stats.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '\"' LINES TERMINATED BY '\n';
我得到的输出是:
+------------------------------------------------------------------------+
| @2 |
+------------------------------------------------------------------------+
| D:/Graph_draw/ASSET_Creation/EXTERNAL/MYSQL_STATS_COLLECTOR/foldrname1 |
+------------------------------------------------------------------------+
ERROR 1 (HY000) at line 2 in file: 'DBQuery1.sql': Can't create/write to file 'C
:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.5\Data\
@2\Stats.csv' (Errcode: 2)
这意味着@ 2没有获得'@ 2 \ Stats.csv'中的值,而是SELECT @ 2;在sql中获取值。
任何帮助?
答案 0 :(得分:0)
问题是,正如您自己所指出的那样,用户定义的变量@ 2未在INTO OUTFILE的文件规范中扩展为其值。 有一种解决方法,使用动态SQL:
SET @stmt = CONCAT(
' SELECT ''INNODB_OS_LOG_WRITTEN'',''QUESTIONS'''
, ' UNION'
, ' SELECT MAX(IF(variable_name = ''INNODB_OS_LOG_WRITTEN'', VARIABLE_VALUE, NULL)) AS INNODB_OS_LOG_WRITTEN'
, ' , MAX(IF(variable_name = ''QUESTIONS'', VARIABLE_VALUE, NULL)) AS QUESTIONS'
, ' FROM information_schema.GLOBAL_STATUS'
, ' INTO OUTFILE ', @2, '/Stats.csv'
, ' FIELDS TERMINATED BY '','''
, ' ENCLOSED BY ''"'''
, ' LINES TERMINATED BY ''\n'''
);
PREPARE stmt FROM @stmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;