user1286151
user1286151

Reputation: 23

Calling MySQL Stored Procedure in Shell Script

Want to Execute a MYSQL Stored Procedure in shell script

Example : Employee_config ('ClientId','Data') is Procedure name in mysql.

When I try to give in the shell script as below

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

So would be great if someone can have an update on the same on how to call MySQL stored procedure from shell script.

Upvotes: 2

Views: 12451

Answers (2)

user3123372
user3123372

Reputation: 744

You can try the below way.

myscript.sh

#!/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"

And execute the script like

./myscript.sh

Upvotes: 5

arcee123
arcee123

Reputation: 211

You can try to call it using the mysql command:

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

you can follow that with a > out.txt to capture the output into a file.

Upvotes: 5

Related Questions