Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to write a script for batch execution of SQL files?
How to write a script for batch execution of SQL files?
Step: (1) Create batch file: Open Notepad and create database connection information (for example, server name, user name, password, database name, etc. ) and *. The sql script to be executed. Please refer to the following "osql Utility" for specific syntax. Change the file extension to. Hit the ball and save it.

Example 1: Specify the relative path of the sql file to be executed.

osql-S admin \ admin-U sa-P sa-d test db-I c:\ test . SQL

discontinue

Example 2: The file to be executed and the bat file are in the same directory, and the relative path is specified.

Set local directory =%~dp0

Osql -S (local)-u sa-p sa-d test-i% localdir% \ test1.sql.

Osql -S (local)-u sa-p sa-d test-i% localdir% \ test2.sql.

Osql -S (local)-u sa-p sa-d test-i% localdir% \ test3.sql.

discontinue

(2) Create sql scripts to be executed, namely, test.sql, test2.sql and test3.sql, and put them in the location specified by example 1 in step (1). In this case, it is the c:\ root directory. If you use Example 2, put the sql and bat files in the same directory.

(3) Double-click *. Bat file and check the running results.