Unknown origin
Overview:
As a DBA, it is often necessary to update the official database with the SQL script provided by the developer, but a more reasonable development process, when the script is submitted to the DBA, there may already be hundreds of SQL files, and there is a sequence of execution, such as the company I am working on, more than 10 customers, one library per customer, But the database structure, stored procedures, views, and so on are exactly the same, each execution of the script (hereinafter referred to as the upgrade), if there are 100 of scripts, then the order is executed thousands of times, this workload is not a person can afford.
Workaround:
There are several ways to deal with this situation:
1, purchase third-party software (generally estimated that few people buy)
2, self-programming a small software to execute, but this logic requirements are relatively high, and the ability to program a certain level, this I do not.
3, using the method described in this article, as to what is, then look:
Use SQLCMD to execute multiple scripts on SQL Server:
Sqlcmd: Use the sqlcmd utility, which can be used at a command prompt, in the Query Editor in SQLCMD mode, in a Windows script file, or in the operating system of a SQL Server agent job (Cmd.exe ), enter Transact-SQL statements, system procedures, and script files in the job step. This utility uses ODBC to perform Transact-SQL batch processing. Detailed syntax (from MSDN) can be found on the web and is not posted here.
Sqlcmd has a very important command:: R, remember that SQLCMD is case-sensitive. When: R discovery is running SQL script, it tells Sqlcmd to put the file referenced in the file into the calling script. This will tell you to stop the current single query. and re-adjust the query, put the query should be associated in the appropriate location. In addition, using the: R command executes multiple scripts in a batch, allowing you to define a separate set of variables to contain all the scripts, but not the go terminator. The introduction of SQLCMD since 2005 can be used to replace the osql tool in the future. If you are unfamiliar with sqlcmd, you can think of it as a command-line tool that can execute T-SQL commands and scripts from the operating system.
In the following example, create 5 SQL files that have an associated effect on the TestDB database. The first script, called Create_db.sql, is used to create a database called TestDB. This script contains 4 other scripts (using the: R command. ), which is used to generate additional tables, table inserts, index creation, and the creation of stored procedures. A. bat file is used to create a sqlcmd command to execute.
The first step: Create a folder under the C drive: C:\Scripts. Then store the script in this folder:
Script 1:create_db.sql
[SQL]View Plaincopyprint?
- /* SCRIPT:CREATE_DB.sql */
- /* Create TestDB Database */
- --This is the main caller for each script
- SET NOCOUNT on
- GO
- PRINT ' Start creating TestDB database '
- IF EXISTS (SELECT 1 from SYS. DATABASES WHERE NAME = ' TestDB ')
- DROP DATABASE TestDB
- GO
- CREATE DATABASE TestDB
- GO
- : OnError exit
- : R c:\Scripts\CREATE_TABLES.sql
- : R c:\Scripts\TABLE_INSERTS.sql
- : R c:\Scripts\CREATE_INDEXES.sql
- : R c:\Scripts\CREATE_PROCEDURES.sql
- PRINT ' Create complete '
- GO
Script 2:create_indexes.sql
[SQL]View Plaincopyprint?
- /* Create an index */
- PRINT ' Start index creation '
- GO
- Use TestDB
- GO
- IF not EXISTS ( SELECT 1
- From SYS. INDEXES
- WHERE NAME = ' ix_employee_lastname ')
- CREATE INDEX ix_employee_lastname on DBO. EMPLOYEE (LASTNAME, FIRSTNAME)
- GO
- IF not EXISTS ( SELECT 1
- From SYS. INDEXES
- WHERE NAME = ' Ix_timecard_employeeid ')
- CREATE INDEX ix_timecard_employeeid on DBO. TIMECARD (EMPLOYEEID)
- GO
Script 3:create_procedures.sql
[SQL]View Plaincopyprint?
- /* Create a stored procedure */
- PRINT ' Creating stored procedure '
- GO
- Use TestDB
- GO
- IF object_id (' get_employee_timecards ') is not NULL
- DROP PROCEDURE DBO. Get_employee_timecards
- GO
- CREATE PROCEDURE DBO. Get_employee_timecards @EMPLOYEEID INT
- As
- SET NOCOUNT on
- SELECT *
- From DBO. EMPLOYEE E
- JOIN DBO. TIMECARD T on e.employeeid = T.employeeid
- WHERE E.employeeid = @EMPLOYEEID
- ORDER by dateworked
- GO
Script 4:create_tables.sql
[SQL]View Plaincopyprint?
- /* Create data Table */
- PRINT ' creating data table '
- GO
- Use TestDB
- GO
- IF object_id (' EMPLOYEE ') is not NULL
- DROP TABLE DBO. EMPLOYEE
- GO
- CREATE TABLE DBO. EMPLOYEE
- (
- EMPLOYEEID INT IDENTITY (1, 1)
- not NULL
- PRIMARY KEY,
- FIRSTNAME VARCHAR (+),
- LASTNAME VARCHAR (a)
- )
- GO
- IF object_id (' TIMECARD ') is not NULL
- DROP TABLE DBO. TIMECARD
- GO
- CREATE TABLE DBO. TIMECARD
- (
- Timecardid INT IDENTITY (1, 1)
- not NULL
- PRIMARY KEY,
- EMPLOYEEID INT not NULL,
- hoursworked TINYINT not NULL,
- Hourlyrate Money is not NULL,
- dateworked DATETIME not NULL
- )
- GO
- DECLARE @TOTAL_TABLES INT
- SET @TOTAL_TABLES = 2
Script 5:table_inserts.sql
[SQL]View Plaincopyprint?
- /* Insert Table Data */
- PRINT ' total TABLES CREATED = ' + CAST (@TOTAL_TABLES as VARCHAR)
- GO
- PRINT ' inserting data to table ' EMPLOYEE '
- GO
- Use TestDB
- GO
- INSERT into DBO. EMPLOYEE
- (FIRSTNAME, LASTNAME)
- SELECT ' JOHN ',
- ' DOE '
- GO
- INSERT into DBO. EMPLOYEE
- (FIRSTNAME, LASTNAME)
- SELECT ' JANE ',
- ' DOE '
- GO
- INSERT into DBO. EMPLOYEE
- (FIRSTNAME, LASTNAME)
- SELECT ' JEFF ',
- ' DOE '
- GO
Step Two: Create a bat file Create_db.bat in the C packing directory to perform sqlcmd:
[Plain]View Plaincopyprint?
- Sqlcmd-e-dmaster-ic:\scripts\create_db.sql
- PAUSE
Step three: Execute the bat file directly under the C drive:
Double-click the file to see:
Before execution, there is no testdb:
In execution:
After execution, the created things are created:
Because the order of execution is already defined in script 1, it executes directly and executes successfully.
Summarize:
Depending on your experience, it's better to develop a batch execution tool that can be used in a small number of scripts.
Use SQLCMD to perform multiple script reprints in SQL Server