我需要使用批处理文件执行多个sql文件。因此,我使用以下代码创建了一个文件 CREATE_DB.sql :
/* SCRIPT: CREATE_DB.sql */
/* BUILD A DATABASE */
-- This is the main caller for each script
SET NOCOUNT ON
GO
PRINT 'CREATING DATABASE'
IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'MSSQLTIPS')
DROP DATABASE MSSQLTIPS
GO
CREATE DATABASE MSSQLTIPS
GO
:On Error exit
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_TABLES.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\TABLE_INSERTS.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_INDEXES.sql
:r C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\CREATE_PROCEDURES.sql
PRINT 'DATABASE CREATE IS COMPLETE'
GO
然后,我使用以下代码创建批处理文件 create_db.bat :
SQLCMD -E -dmaster -i"C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql"
PAUSE
但是,双击批处理文件时会显示以下错误:
Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file 'C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql'.
我认为这是由于文件路径中的空格,但是如何摆脱这个错误?