| DosTips.com ... for WinXP |
|
Last update: Jan 19, 2008 |
|
OSQL.EXE - Run SQL script from DOS Batch
SQL script and dos batch script in one file, the One-File Solution
|
OSQL.EXE - Run SQL script from DOS Batch, passing parameters
Run SQL scripts with parameters passed in from the batch script.
|
Description: | Embed FTP script into a batch script. Add this line at the beginning of the FTP script: @ftp -i -s:"%~f0"&GOTO:EOFThe "FTP -s:ftpscript.txt" option executes a FTP script wheres "%~f0" resolved to the name of the running batch file.
"GOTO:EOF" ends the batch script and makes sure the FTP script doesn`t run as part of the batch.
| ||
Script: | |||
Script Output: |
|
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
|
Description: | Now we can embed SQL queries into a batch file. But how can we pass arguments from the batch script into SQL? This can be done using a temporary table. Temporary tables live as long as the connection to the SQL Server. But how can we fill a temporary table with runtime data and execute the embedded SQL script without creating two separate SQL Server connections by calling OSQL.EXE twice? The trick is that OSQL allows to use the -i and -q option at the same time whereas:
| ||
Script: | Download: Batch4SQL2.bat
|
Description: | It`s nice not to have to type "perl -s Batch4Parl.pl" into the command line and rather being able to just double click a Perl script in Explorer. The trick of renaming the Perl .pl to a batch .bat file and wrapping a batch script around the Perl script is well known under Perl monks. However the solution I have seen so far needed batch code before and after the Perl script where as the solution presented below only needs some lines of DOS at the top. The added DOS script is generic and works independent from the name of the file. When running the batch the DOS command interpreter will read the first lines and execute the file itself in Perl context. |
Script: |
Description: | This example works just as the previews one but will wait 4 seconds before the application finally closes. This is just enough time to inspect the screen output before the window vanishes. The delay can probably be done much easier in Perl, but somebody just starting on Perl might find this still useful. | ||
Script: | Download: Batch4Perl2.bat
|