Want to Execute a MYSQL Stored Procedure in shell script
Example : Employee_config ('ClientId','Data') is Procedure name in my sql
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
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.
You can try the below way...
#!/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
Related
Command for executing the MySQL stored procedures using command prompt also required to reduce the size of output history on a console window.
Update:
There is already a similar question with excellent answers on the CALL'ing part.
To run stored procedures from the command line, use the -e option of the mysql client. Example, to call a SP "foo", from bash, you'd do:
bash> mysql -e "call foo()" dbname
If you do not want the command to be stored in the history file, add a space before the mysql command, thusly:
bash> mysql -e "call foo()" dbname
^
|_ space
Server : MySQL
Query : SQL
Requirement : Shell Script
How do I run a sql query using shell script for mysql server.
This will help me in automating the task.
mysql -u USER -p PASSWORD -h MYSQLSERVERNAME -e 'select * from foo...' database-name
I am creating a database in a shell script using
mysql -u$username -p$password -e " create database testdb"
I want to store the result of this statement to make decisions in shell script based on its success or failure.
Even during insert statements i do not get any return value
how can is store the result after executing create database / insert statements in shell script?
use the following command substitution syntax:
var=$(command-name-here arg1 arg2...)
So for your command you can do something like this,
output=$(mysql -u$username -p$password -e " create database testdb")
echo "$output"
I am using MySQL: 5.1.69
I have created stored procedures and called them many times. In this case, I appear to have hit a snag related to escape characters in the schema name.
our MySQL database / schema is named like this: www.company.com
Due to this, we must always escape the database name like this: `www.company.com`.table_name
I have created a procedure from within MySQLWorkbench
CREATE PROCEDURE usp_backfill_data
(p_var1 INT(11)
,p_var2 DATETIME)
BEGIN
/*do lots of work here*/
END
And from within MySQL Workbench I am able to call the procedure using:
use `www.company.com`;
CALL usp_backfill_data(5000, '2012-01-01');
Under these conditions the procedure works exactly as expected.
However, when I try to call the procedure from the command line:
%> mysql -uuser -ppassword -Dwww.company.com -e "CALL usp_backfill_data(5000, '2012-01-01');"
or when I try
%> mysql -uuser -ppassword -e "CALL \`www.company.com\`.usp_backfill_data(5000, '2012-01-01');"
or when I log into the mysql command line and use:
mysql> use `www.company.com`;
mysql> CALL usp_backfill_data(5000, '2012-01-01');
I always get the following error:
ERROR 1305 (42000) at line 1: PROCEDURE www.company.com.usp_backfill_data does not exist
I am hoping that there is something super obvious that I'm overlooking here.
Thank you very much for your time
To escape database name in command line use double quotes
$ mysql -uuser -ppassword -D"www.company.com" -e "CALL usp_backfill_data(5000, '2012-01-01');"
^ ^
Other two methods
$ mysql -uuser -ppassword -e "CALL \`www.company.com\`.usp_backfill_data(5000, '2012-01-01');"
and
$ mysql -uuser -ppassword
mysql> USE `www.company.com`;
Database changed
mysql> CALL usp_backfill_data(5000, '2012-01-01');
work for me just fine
I am trying to create a batch script that would connect to a mySQL database and issue a delete command:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback
delete from competency_question_answer;
I will run this script providing the password as a command-line argument, but all this script does is, connects to the database, and the mysql> prompt will be shown. After I exit from mysql, the rest of the batch commands get to execute (and fail, no surprise).
How can I pass the SQL commands from the batch script to the mysql console? Is this even possible?
You need to use command line tools. I don't know if there exists any for MySQL but for SQL there is SQLCMD and for Oracle there is OSQL.
What you can also do is something like this.
mysql -uuser -ppass < foo.sql
Where foo.sql is the commands you want to execute.
You may need to connect multiple times:
#echo off
echo Resetting all assessments...
mysql -hlocalhost -urdfdev -p%1 rdf_feedback -e delete from competency_question_answer;
Alternatively, you should be able to put all your commands in a separate file such as input.sql and use:
mysql -hlocalhost -urdfdev -p%1 rdf_feedback <input.sql
echo "delete from competency_question_answer;" | mysql -hlocalhost -ur... etc.
Putting multiple sets of commands into .sql batch files works best, and you can execute multiples of these in the .bat file.