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.
LVL 2
learningtechnologiesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

markterryCommented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
markterryCommented:
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.
0
learningtechnologiesAuthor Commented:
My apologies - I should have specified - this is running on a Linux server.  Thank you very much for your response -
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

learningtechnologiesAuthor Commented:
. . . and that's an excellent thought on logging the output.  Thank you.
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
Hello,

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.
0
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 -
0
markterryCommented:
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.  
0
learningtechnologiesAuthor Commented:
I appreciate your follow up, markterry
0
learningtechnologiesAuthor Commented:
Thank you very much for helping me think about this and come up with a plan.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.