OSQL.EXE - Run SQL script from DOS Batch

SQL script and dos batch script in one file, the One-File Solution

Description:

Embedding SQL script within a batch script is just as easy. The following batch script executes itself in SQL context. The trick is the GOTO command in the first line of the script. When executing GOTO START in batch context than the command processor will jump to the label ":START" and execute the batch script. The batch script will then run the OSQL.EXE using the batch file itself as SQL file argument to be executed. When subsequently executing the GOTO START line in SQL context, the query language processor will jump to the label "START:" and execute the SQL queries. In fact the file can be opened and executed in Query Analyzer as is, since the batch script in the file looks like a comment to the query language processor.

Script: Download: Batch4SQL.bat  
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
GOTO START
-- DOS jumps to the ':START' label
-- SQL jumps to the 'START:' label
-- Source http://www.dostips.com

/* Begin of SQL comment, this makes the BATCH script invisible for SQL
:: BATCH starts below here
:START
@echo off
CLS

OSQL.EXE -n -E -w 65536 -d NORTHWIND -i "%~f0"

PAUSE&GOTO:EOF
*/

-- SQL starts below here
START:
GO
SELECT * FROM AUTHOR
GO