Execute Stored Procedure from Script

learningtechnologies used Ask the Experts™
Hello there -

How can I execute a stored procedure from a script?

I can run backups and do other tasks that I have permissions to execute from a script that I have cron run everyday - so at least I have that part down.

What I don't understand is how to pass the commands to use the stored procedure from a script.

Thanks in advance for any and all thoughts, tips, ideas or vague notions you may be able to lend . . .

/David C.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
yes, assuming you are using windows server, you can do this using SQLCMD a cheap solution.

you can save a sql script file which would say
EXEC sp_Proc Param1, param2

Open in new window

and execute that from command line saved in a bat file.

Bat file line would look like:
SQLCMD -S SQLServer\Instance -E -d DBName -i SQLScript.sql

Usually, I like to kind of log the output, so I pipe it:
SQLCMD -S SQLServer\Instance -E -d DBName -i SQLScript.sql >SQLScript.LOG
to clarify, you would save the SQL file with any code to execute, save the bat file calling SQLCMD on the SQL Script, the schedule the batch file to run in task scheduler.
My apologies - I should have specified - this is running on a Linux server.  Thank you very much for your response -
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

. . . and that's an excellent thought on logging the output.  Thank you.

If you have the script for creating tables after the statements of creating tables you can also creating the stored procedures at the first time of creating the database with the help of script files.
I believe I may be getting an idea based on the two responses so far - please let me know if this sounds reasonable.

So . . . I should store the MySQL commands that I want executed in a file named something like 'commands.sql'.  The commands would be the commands I would use after starting up the mysql command line client.

Then, I would pass the file to the mysql command line client using the script file something like this:

shell> mysql -u <acct> -p<pass> dbname < commands.sql

I would need to ensure that the account that I use has sufficient privileges to use the stored procedure that has already been created.

Can I send the results out to a file like this?

shell> mysql -u <acct> -p<pass> dbname < commands.sql > results.txt

Thanks for helping me think about this -
Sorry, i thought the cron was off, and didnt notice the MySql thought it was MS SQL. What you have planned there looks good to me, but I dont have linux so I dont know for sure.  
I appreciate your follow up, markterry
Thank you very much for helping me think about this and come up with a plan.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial