我想写一个简单的Windows批处理脚本,它要求输入某些valiables并使用这些输入在批处理文件中运行sql查询。例如
@echo off
mysql "-uroot" -p -h "xx.xx.xx.xx" "abc"
SET /P ntid= Please enter the id of the user:
select * from tbl_employeedetails where empntid=%ntid%;
我想在控制台中请求empntid然后接受它并在sql中替换它然后运行它。 当我运行这个时,我收到错误:
mysql "-uroot" -p -h "xx.xx.xx.xx" "abc"
Enter password: ********
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '/P ntid= Please enter the NT id of the user:
select * from tbl_employeedetail' at line 1
我实际上必须使用来自用户的输入运行多个查询,这将在控制台中提示
答案 0 :(得分:0)
但我用一个小技巧来做到这一点
这是一个示例批处理文件
@echo off
set /p name=Enter Name:
set /p ts=Enter Ts:
set /p id=Enter ID:
@echo update `table1` set `t1`.`name` = "%name%" , `t1`.`ts` = "%ts%" where `t1`.`id` = "%id%";> test.sql
set /p name=Enter Name 2:
set /p ts=Enter Ts 2:
set /p id=Enter ID 2 :
@echo update `t1` set `t1`.`name` = "%name%" , `t1`.`ts` = "%ts%" where `t1`.`id` = "%id%";>> test.sql
set /p name=Enter Name 3:
set /p ts=Enter Ts 3:
set /p id=Enter ID 3:
@echo update `t1` set `t1`.`name` = "%name%" , `t1`.`ts` = "%ts%" where `t1`.`id` = "%id%";>> test.sql
set /p name=Enter Name 4:
set /p ts=Enter Ts 4:
set /p id=Enter ID 4:
@echo update `t1` set `t1`.`name` = "%name%" , `t1`.`ts` = "%ts%" where `t1`.`id` = "%id%";>> test.sql
mysql -uroot -p9272 -hlocalhost test < test.sql
del test.sql
希望这可以提供帮助。