问题
I need to execute multiple sql files using a batch file. So, I created a file CREATE_DB.sql with following code:
/* 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
Then, I create a batch file create_db.bat with following code:
SQLCMD -E -dmaster -i"C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql"
PAUSE
But, on double-clicking the batch file it shows the following error:
Sqlcmd: Error: Syntax error at line 17 near command ':r ' in file 'C:\Users\XYZ\Documents\SQL Server Management Studio\BATCheck\create_db.sql'.
This, I think, is due to the Spaces in the file paths, but how to get rid of this error?
来源:https://stackoverflow.com/questions/29182452/sqlcmd-error-syntax-error-at-line-17-near-command-r-in-file