Here’s a quick little trick with the command line: consider a situation where you have a folder of .sql scripts that are used to update or create a new database. One way you can easily run all of these scripts against the database is by using the osql command line tool and a ‘for’ loop. I found this tip on some other site a while back, and unfortunately can’t remember where.
for %z in (”c:\DBInstallFolder\*.sql”) do osql -E -n -S MyDatabaseServerName -d MyDataBaseName -i “%z”
Pow. WIth one click of a bat file, you can run a whole folder of .sql scripts.
Oh, one other bonus dos command: when you generate sql install scripts from a database in Enterprise Manager (or Management Studio), it outputs them as ‘.prc’ files. I prefer the more generic ‘.sql’ extension. Change the file extension for all files in a folder like so:
ren *.prc *.sql
It’s worth it to get to know all the things you can do at the command line. It’s a lot of power at a keyboard jump away (windows-r, cmd, enter).



0 Responses to “OSQL: Calling SQL From Command Line”