在Shell脚本中调用MySQL存储过程

问题描述 投票:0回答:2

想要在shell脚本中执行MYSQL存储过程

示例:

Employee_config ('ClientId','Data')
是mysql中的过程名称。

当我尝试输入如下所示的 shell 脚本时

CALL Employee_config ('ClientId','Data') we are getting CALL: command not found
EXECUTE Employee_config ('ClientId','Data') we are getting EXECUTE: command not found

如果有人可以更新如何从 shell 脚本调用 MySQL 存储过程,那就太好了。

mysql shell stored-procedures
2个回答
5
投票

你可以尝试使用mysql命令调用它:

mysql -u root –ppassword -e 'call test_procedure();' Databasename

您可以使用

> out.txt
将输出捕获到文件中。


4
投票

您可以尝试以下方法...

#!/usr/bin/bash
#Script to run automated sql queries
#Declaring mysql DB connection 

MASTER_DB_USER='root'
MASTER_DB_PASSWD='root'
MASTER_DB_PORT='3160'
MASTER_DB_HOST='192.168.0.0'
MASTER_DB_NAME='MyDB'


startDate='2018-03-09'
endDate='2018-03-09'

#Prepare sql query
SQL_Query1='select * from mytable limit 1'
SQL_Query2='call carServicedQry'
SQL_Query3='call carServicedQry2("2018-03-09","2018-03-09")'



#mysql command to connect to database

MYSQL -u$MASTER_DB_USER -p$MASTER_DB_PASSWD  -D$MASTER_DB_NAME <<EOF 
$SQL_Query2   
EOF
echo "End of script"

并执行脚本,如./myscript.sh

© www.soinside.com 2019 - 2024. All rights reserved.