OSQL: Calling SQL From Command Line

02Nov06

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”


  1. No Comments

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.

Quote selected text




 

About

My name is Dylan Marks. I'm a programmer and tech consultant focused on web application development, typically using ASP.NET, javascript and web standards-based technologies. I hope to share some of the useful tips and tricks I've discovered while making things work. I live in Edmonton, Alberta, Canada, working remotely for clients across North America. Email Me.