SoFunction
Updated on 2025-04-09

Code to execute sql statements using BAT batch processing

1. Save Sql to be executed in a file, here.
2. Create a new batch file with the extension .bat, enter the following command and save it, double-click the .bat file, and the system will automatically execute the statement:

Copy the codeThe code is as follows:

osql -S gdjlc -d TestDB -U sa -P 1 -i

See the following osql parameters
=======================================================================

E:\>osql /?
Microsoft (R) SQL Server Command Line Tools
Version 10.0.1600.22 NT INTEL X86
Copyright (c) Microsoft Corporation. All rights reserved.

Note: osql does not support all features of SQL Server 2008.
Please use sqlcmd. For more information, see SQL Server Books Online.

Usage: osql                       [-U Login ID]            [-P Password]
[-S Server]                         [-E Trusted Connection]
[-d Use database name]        [-l Login timeout value]       [-t Query timeout value]
[-h Title]                             [-s column separator]            [-w column width]
[-a Packet Size]                 [-e Echo Input]            [-I Allow quotation marks]
[-L List Server]               [-c Command End]          [-D ODBC DSN Name]
[-q "Command Line Query"]           [-Q "Command Line Query" and exit]
[-n Delete numbering method]            [-m Error level]
[-r Message sent to stderr] [-V Severity level]
[-i input file]                      [-o output file]
[-p Print statistics]           [-b Abort batch processing when an error occurs]
[-X[1] Disable the command, [Show warning while exiting]]
[-O Use old ISQL behavior to disable the following items]
<EOF> Batch Processing
Automatically adjust the console width
Wide message
The default error levels are -1 and 1
[-? Show syntax summary]

Execute SQL command instance through batch processing:

Execute the SQL command through batch processing:

1. File content:

      osql -E -S -i C:\TempDB\

 

2. File content:

Copy the codeThe code is as follows:

use master
if exists (select * from sysdevices where name='TruckDB')
       EXEC sp_dropdevice 'TruckDB'
Else
       EXEC sp_addumpdevice 'disk','TruckDB', 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\'
restore database TruckDB
from disk='c:\TempDB\TruckDB'
with replace
 

Export data to txt:

Copy the codeThe code is as follows:

EXEC master..xp_cmdshell 'bcp "test..mintest" in d:\ -c -Sd02 -Usa -P'
EXEC master..xp_cmdshell 'bcp "test..min" out d:\ -c -Sd02 -Usa -P'

In many cases, it is not worth it to update the database for customers.

After a trip, the customer does not know how to operate, and the remote operation network speed cannot keep up with it... At this time, you can save the SQL statement you want to update into a file and write a batch process on the command line.

In the state, call the query analyzer to execute the statements in this sql file.

The following is the explanation of the command line parameters of the query analyzer (isqlw) in the SQLServer help:

isqlw Practical Tools(SQL Query Analyzer)Allows you to enter Transact-SQL Statement、System stored procedures and script files。 By setting shortcuts or creating batches

Files that can start the preconfigured SQL query analyzer.

grammar
isqlw
    [-?] |
     [
         [-S server_name[instance_name]]
         [-d database]
         [-E] [-U user] [-P password]
         [{-i input_file} {-o output_file} [-F {U|A|O}]]
         [-f file_list]
         [-C configuration_file]
         [-D scripts_directory]
         [-T template_directory]
     ]

parameter
-?

Display usage information.

-S server_name[instance_name]:

Specify the Microsoft® SQL Server™ 2000 instance to connect to. Specifies the server_name used to connect to the default instance of SQL Server 2000 on this server.

Specifies the server_nameinstance_name used to connect to the SQL Server 2000 named instance on this server. If no server is specified, isqlw will connect to the local

The default instance of SQL Server on the computer. This option is required when executing isqlw from a remote computer on the network.

-d database

When isqlw is started, a USE database statement is issued. The default value is the user's default database.

-E

Use a trust connection without requesting a password.

-U user

User login ID. Login ID is case sensitive.

-P password

It is the login password. The default setting is NULL.

-i input_file

Identifies a file that contains a batch of SQL statements or stored procedures. The -i and -o options must be specified at the same time. If the -i and -o options are specified, the query in the input file will be executed, and

Save the result to the output file. The user interface is not displayed during query execution. When execution is completed, the process exits.

-o output_file

Identifies the file that receives the output from isqlw. The –i and –o options must be specified at the same time. If the -i and -o options are specified, the query in the input file will be executed and the end will be terminated

Save the result to the output file. The user interface is not displayed during query execution. When execution is completed, the process exits. If -F is not used to specify the file format, the output file is used

Same type as input file.

-F {U|A|O}

It is the format of input files and output files. Values ​​include Unicode, ANSI, and OEM. If -F is not specified, automatic mode is used (if the file is marked in Unicode format,

Open in Unicode format; otherwise, open the file in ANSI format).

-f file_list

Load the listed files into the SQL query analyzer. Using the -f option, one or more files can be loaded (file names separated by a single space). If multiple files are specified

, then open these files with the same connection context. The file name can contain the directory path where the file resides. Wildcard characters such as the asterisk in C:Test*.sql

(*)。

-C configuration_file

Use the settings specified in the configuration file. Other parameters explicitly specified at the command prompt will override the settings in the corresponding configuration file.

-D scripts_directory

Rewrites the default storage script directory specified in the registry or in the configuration file specified with –C. This value is not retained in the registry or configuration file. To query analysis in SQL

To view the current value of this option in the machine, click the Tools menu, and then click the Options command.

-T template_directory

Rewrites the default template directory specified in the registry or in the configuration file specified with –C. This value is not retained in the registry or configuration file. To be in SQL Query Analyzer

To view the current value of the option, click the Tools menu, and then click the Options command.


Comments

The isqlw utility can be used when there is a user interface or without a user interface. To run isqlw without a user interface, specify valid login information (with trust
Connect or validly log in to SQL Server 2000 instance) and input and output files. isqlw will execute the contents of the input file and save the result to the output file
middle.
If no input and output files are specified, isqlw will run interactively and start the SQL query analyzer. If valid login information is specified, isqlw will directly connect to SQL
Server 2000 instance. If the specified connection information is insufficient, the Connect to SQL Server dialog box appears.
isqlw and SQL query analyzers use the ODBC API. This utility uses the Microsoft® SQL Server ODBC driver default settings for SQL-92.
Example

A. Execute SQL statements
This example uses Windows authentication to connect to the pubs database on MyServer and executes the file. The results are saved in a file. These
The file is opened as a Unicode file.
isqlw -S MyServer -d pubs -E -i -o -FU
 

B. Use wildcards
This example loads two files into the SQL query analyzer. Connect to the local server using Windows authentication.
isqlw -d pubs -E -f "c:Program FilesMicrosoft SQL " "c:Program FilesMicrosoft
SQL "
 

C. Load multiple files
This example loads all .sql files into the SQL query analyzer. All connections use Windows authentication and point to the pubs database on the local server.
isqlw -d pubs -E -f "c:Program FilesMicrosoft SQL ServerMSSQLInstall*.sql"
 

D. Use Unicode files
This example connects to MyServer (pubs database) and executes SQL statements in input_file. The execution results are stored in output_file.
isqlw -S MyServer -d pubs -U sa -P -i input_file -o output_file