1

I'm having a really hard time believing this question has never been asked before, it MUST be! I'm working on a batch file that needs to run some sql commands. All tutorials explaining this DO NOT WORK (referring to this link:Pass parameters to sql script that someone will undoubtedly mention)! I've tried other posts on this site verbatim and still nothing is working.

The way I see it, there are two ways I can approach this: 1. Either figure out how to call my basic MYSQL script and specify a parameter or.. 2. Find an equivalent "USE ;" command that works in batch

My Batch file so far:

:START
@ECHO off

:Set_User
set usrCode = 0

mysql -u root SET @usrCode = '0'; \. caller.sql

Simply put, I want to pass 'usrCode' to my MYSQL script 'caller.sql' which looks like this:

USE `my_db`;
CALL collect_mismatch(@usrCode);

I know that procedures are a whole other topic to get into, but assume that the procedure is working just fine. I just can't get my parameter from Batch to MYSQL.

Ideally I would like to have the 'USE' & 'CALL' commands in my Batch file, but I can't find anything that let's me select a database in Batch before CALLing my procedure. That's when I tried the above link which boasts a simple command line entry and you're off to the races, but it isn't the case.

Any help would be greatly appreciated.

2
  • Remove spaces in your SET Batch command: set usrCode=0 Commented Oct 27, 2014 at 21:37
  • Haha, your comment ended up helping me out in the end. I tried everything in the answer below and totally forgot about the Batch variable rules. Thank you! Commented Oct 27, 2014 at 23:36

1 Answer 1

4

This will work;

echo SET @usrCode = '0'; > params.sql
type params.sql caller.sql | mysql -u root dbname
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome, thanks for your speedy reply! :) Only question I have, because I want to learn from stuff like this. What is the function of the " | " between the type command and calling mysql?
It's a pipe, it sends the printed data on the left (stdout) to the input stream of the right (stdin).
@harvey works great for one argument, how to do it for multiple arguments? Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.