Groups | Blog | Home
all groups > sql server msde > november 2004 >

sql server msde : Run Multiple Stored Procedures in order with a script??


Nigel Rivett
11/21/2004 1:55:10 PM
See
http://www.nigelrivett.net/s_ProcessAllFilesInDir.html
It will execute a proc for each file in a directory in order of the file name.
Create a proc which takes two parameters, file path and file name and
executes a osql command to run that file with output to a file.
Set @ProcSp to be this proc and you are done.


[quoted text, click to view]
Tom
11/21/2004 3:32:16 PM
Hello. I have to run many sql scripts each day. Sometimes I'll have 100+
scripts to run. The scripts always come in a folder numbered in the order
they need to be run (001 - ScriptA, 002 - ScriptB, etc.).

I've been running them all manaully one by one so that I can look for errors
in the results. I'd love to be able to point a batch file to the folder
with all the scripts, have them run one by one, and have the results output
in one file with:

Results.txt:
001 - ScriptA.sql
Command Completed Successfully
----
002 - ScriptB.sql
Command Completed Successfully
---
003 - ScriptC.sql
Error in line 1...etc
---
004 - ScriptD.sql
Command Completed Successfully
---
etc.


I have tried piping them using c:\Scipts\*.sql > c:\AllScripts.sql

However, this is often causes problems as sometimes the end of one script
will join directly to the start of the first script making statements that
don't exist (EndscriptGo).. two statements joint into one. Or other times
scripts will be left out for some reason. Piping has caused many problems
and isn't trusted anymore.

I tried using osql
OSQL -Usa -Ppass -Sdb123 -iC:\scripts\001scriptA.sql -oC:\logs\001scriptA.lo
g
but I don't know how to run scripts 001 through say 108 in order, or how to
have them output into one file.

Is there a way to use this or something else (like run 001*.sql, then run
001+1*.sql [I have no idea how to program!) so that I can just point it to
the folder with the numbered scripts and have them run one by one in order??


Any help is REALLY REALLY appreciated!!!!


Thank you


Andrew J. Kelly
11/21/2004 3:50:53 PM
Why not put the names of the scripts in a table and loop thru with a cursor
and call oSql one script at a time?

--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Tom
11/21/2004 4:05:49 PM
Okay, that is way over my head!

I would have to create a new DB for it because I can't add a new table to
the live DB.

If I tried adding all the script names to a table wouldn't I have to type
all the names of the scritps in the table? If there's a way to automate
that I could try it, but it sounds like it could end up being as manual a
process as running them one by one.

What do you think?

Thanks




[quoted text, click to view]

Tom
11/21/2004 7:43:54 PM
Awesome, I'll check it out!

Thanks so much Nigel!


Tom

[quoted text, click to view]

Tom
11/21/2004 8:14:59 PM
Nigel, is there an output file in this?

Thanks,

Tom


[quoted text, click to view]

Tom
11/21/2004 8:19:29 PM
Nigel,
How do I create the @procSP and what exactly do I put in it in this case?

Thanks,


Tom

[quoted text, click to view]

David Gugick
11/22/2004 3:01:35 AM
[quoted text, click to view]

Yes, but only the first time...

--
David Gugick
Imceda Software
Larry
11/22/2004 12:17:03 PM
Consider this:

1. Create a text file with the .sql files listed one per line.
2. Create a command/batch file to submit each to osql.exe
3. Have osql send results to the screen, but instead redirect results to a
logfile

For example:

text.txt contents
-----------------
sqlscript1.sql
sqlscript2.sql
sqlscript3.sql

sqlcommand.cmd contents
-----------------------
for /F %%i IN (text.txt) DO osql -Usa -Ppass -Sdb123 -i%%i >> logfile.txt

So, for each file in text.txt, submit the osql script file and redirect to
logfile.

Check out Windows Help on the For command, it's quite handy. I suggest using
the text.txt file so you can edit which scripts run and in which order, in
case your numeric sequence needs to be reordered.

Larry


[quoted text, click to view]

Vinay
11/22/2004 1:51:57 PM
Have you thought about using DTS?. The Active-X filesystem
object would help you figure out the file names and I'm
sure DTS would help you setup a loop to execute your
programs.

Vinay
vinaydottoomuatschwabptdotcom
[quoted text, click to view]
AddThis Social Bookmark Button