Execute Stored Procedure from Script

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.
Who is Participating?
markterryConnect With a Mentor Commented:
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
markterryConnect With a Mentor Commented:
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.
learningtechnologiesAuthor Commented:
My apologies - I should have specified - this is running on a Linux server.  Thank you very much for your response -
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

learningtechnologiesAuthor Commented:
. . . and that's an excellent thought on logging the output.  Thank you.
SAMIR BHOGAYTAConnect With a Mentor Freelancer and IT ConsultantCommented:

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.
learningtechnologiesAuthor Commented:
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 -
markterryConnect With a Mentor Commented:
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.  
learningtechnologiesAuthor Commented:
I appreciate your follow up, markterry
learningtechnologiesAuthor Commented:
Thank you very much for helping me think about this and come up with a plan.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.