How to Query a Database With a Batch File

No doubt there are times when you need to query MSSQL but Query Analyzer isn’t at your disposal. The answer? Use a simple batch file! OSQL is a powerful utility that should already exist on your Windows XP laptops/workstations. If not, it’s easy enough to copy there. Copy the following batch file and with the power of OSQL, your query is returned. Use Blat and email the results to you on a schedule, if you so desire.

@echo off
TITLE Delete From Table SomeTable During Your Upgrade
color 1F
echo deleting SomeTable records…
osql -dyourdatabasename -Usa -Pyourpassword -Q”delete from sometable”
echo Completed. You may now exit this batch file.

Needless to say, the pause isn’t necessary if you want this to be transparent. As always though.. it works well for testing.

Need to add a few more sql lines in but the batch file is limiting? You can simply call a SQL script in this way:

osql -dyourdatabasename -Usa -Pyourpassword -iupdatesql.sql -n

Using that you can insert a sql script very easily by formatting in a way such as this:

exec (‘print ”SomeTableName count:”’)
exec (‘select count(*) from SomeTableName’)
exec (‘print ””’)
exec (‘print ””’)
exec (‘select substring(DBName,1,15) As SomeTableName_Database from ##SomeTempTable_SomeTableName’)

Submit a comment

CommentLuv badge

reverse phone lookupTattoo DesignsSEO
My New Amazon Script Is Close!
A new php script for sale is coming!
Sign up to get notified