使用SQLCMD运行SQL文件时出现“Incorret语法附近...”错误

时间:2022-02-16 01:37:47

I have this command that found on the MSDN. but it throws syntax error. Is there any keywords that i missed? I have read many forums but couldn't find the solution. I also visit guide on MSDN. but nothings happen.

我有这个在MSDN上找到的命令。但它会引发语法错误。有没有我错过的关键字?我读了很多论坛但找不到解决方案。我也访问了MSDN上的指南。但是没有发生。

 sqlcmd -S SERVER\SQL2008R2 -i sqlfile.sql

使用SQLCMD运行SQL文件时出现“Incorret语法附近...”错误

3 个解决方案

#1


2  

You need to run this on Command Prompt and not inside sqlcmd:

您需要在命令提示符上运行此命令,而不是在sqlcmd中运行:

sqlcmd -S SERVER\SQL2008R2 -U UserName -P Password -i sqlfile.sql

sqlcmd -S SERVER \ SQL2008R2 -U UserName -P Password -i sqlfile.sql

i.e. C:\>sqlcmd -S SERVER\SQL2008R2 -U UserName -P Password -i sqlfile.sql

即C:\> sqlcmd -S SERVER \ SQL2008R2 -U UserName -P Password -i sqlfile.sql

#2


0  

Your sqlcmd looks fine.

你的sqlcmd看起来很好。

The error message relates to the SQL within sqlfile.sql!

错误消息与sqlfile.sql中的SQL有关!

To prove the point, change the contents of the file to something really simple e.g.

为了证明这一点,将文件的内容更改为非常简单的内容,例如

SELECT 'Success!' As result

Then run the sqlcmd command again.

然后再次运行sqlcmd命令。

#3


-1  

This can be caused by a single quote in block comments within a TSQL statement in the SQLCMD Editor. This may be a bug because the problem does not occur with the normal SQL Editor

这可以通过在SQLCMD编辑器TSQL语句中的块注释单引号引起。这可能是一个错误,因为普通的SQL编辑器不会出现问题

To confirm this, in SSMS, click on New Query to open the SQL Editor. Type :CONNECT Hostname\SQLInstance
replacing Hostname\SQLInstance with your values for a named instance or just your Hostname if you a using a default SQLInstance.

要确认这一点,请在SSMS中单击“新建查询”以打开SQL编辑器。键入:CONNECT Hostname \ SQLInstance将Hostname \ SQLInstance替换为您的命名实例的值,如果您使用的是默认的SQLInstance,则只替换您的Hostname。

Switch to SQLCMD mode by clicking on the Query menu and selecting SQLCMD mode.

单击“查询”菜单并选择“SQLCMD”模式,切换到SQLCMD模式。

You should see the :CONNECT text you entered change to a grey background.

您应该看到:您输入的CONNECT文本更改为灰色背景。

This confirms you are now using the SQLCMD editor/parser.

这确认您现在正在使用SQLCMD编辑器/解析器。

Paste the following below the :CONNECT statement, replacing with a valid db name to which you have appropriate permissions.

将以下内容粘贴到:CONNECT语句下,替换为您具有适当权限的有效数据库名称。

USE GO

CREATE VIEW [dbo].vw_Test AS ///* Let's test use of single quote in block comments. //*/ SELECT 1 + 1 AS SimpleSum; GO PRINT 'View vw_Test created'

CREATE VIEW [dbo] .vw_Test AS /// *让我们在块注释中测试单引号的使用。 // * / SELECT 1 + 1 AS SimpleSum; GO PRINT'查看vw_Test已创建'

Now, try compiling the view e.g. by clicking the F5 key. You will get an error message similar to this.

现在,尝试编译视图,例如单击F5键。您将收到与此类似的错误消息。

Connecting to ... Msg 102, Level 15, State 1, Procedure vw_Test, Line 16 Incorrect syntax near 'GO'. Disconnecting connection from ...

连接到...消息102,级别15,状态1,过程vw_Test,行16'GO'附近的语法不正确。断开与...的连接

If you remove the single quote by changing: Let's test to Lets test

如果您通过更改删除单引号:让我们进行测试以进行Lets测试

..you get a healthy message:

..你得到一个健康的信息:

Connecting to ... View vw_Test created Disconnecting connection from ...

正在连接...查看vw_Test已创建断开与...的连接

When you are using sqlcmd to run multiple external sql files, you may also encounter the same problem i.e. the separate files compile without problem in SSMS but when invoked from sqlcmd...it complains.

当您使用sqlcmd运行多个外部sql文件时,您可能也会遇到相同的问题,即SSMS中的单独文件编译没有问题,但是当从sqlcmd调用时...它会抱怨。

#1


2  

You need to run this on Command Prompt and not inside sqlcmd:

您需要在命令提示符上运行此命令,而不是在sqlcmd中运行:

sqlcmd -S SERVER\SQL2008R2 -U UserName -P Password -i sqlfile.sql

sqlcmd -S SERVER \ SQL2008R2 -U UserName -P Password -i sqlfile.sql

i.e. C:\>sqlcmd -S SERVER\SQL2008R2 -U UserName -P Password -i sqlfile.sql

即C:\> sqlcmd -S SERVER \ SQL2008R2 -U UserName -P Password -i sqlfile.sql

#2


0  

Your sqlcmd looks fine.

你的sqlcmd看起来很好。

The error message relates to the SQL within sqlfile.sql!

错误消息与sqlfile.sql中的SQL有关!

To prove the point, change the contents of the file to something really simple e.g.

为了证明这一点,将文件的内容更改为非常简单的内容,例如

SELECT 'Success!' As result

Then run the sqlcmd command again.

然后再次运行sqlcmd命令。

#3


-1  

This can be caused by a single quote in block comments within a TSQL statement in the SQLCMD Editor. This may be a bug because the problem does not occur with the normal SQL Editor

这可以通过在SQLCMD编辑器TSQL语句中的块注释单引号引起。这可能是一个错误,因为普通的SQL编辑器不会出现问题

To confirm this, in SSMS, click on New Query to open the SQL Editor. Type :CONNECT Hostname\SQLInstance
replacing Hostname\SQLInstance with your values for a named instance or just your Hostname if you a using a default SQLInstance.

要确认这一点,请在SSMS中单击“新建查询”以打开SQL编辑器。键入:CONNECT Hostname \ SQLInstance将Hostname \ SQLInstance替换为您的命名实例的值,如果您使用的是默认的SQLInstance,则只替换您的Hostname。

Switch to SQLCMD mode by clicking on the Query menu and selecting SQLCMD mode.

单击“查询”菜单并选择“SQLCMD”模式,切换到SQLCMD模式。

You should see the :CONNECT text you entered change to a grey background.

您应该看到:您输入的CONNECT文本更改为灰色背景。

This confirms you are now using the SQLCMD editor/parser.

这确认您现在正在使用SQLCMD编辑器/解析器。

Paste the following below the :CONNECT statement, replacing with a valid db name to which you have appropriate permissions.

将以下内容粘贴到:CONNECT语句下,替换为您具有适当权限的有效数据库名称。

USE GO

CREATE VIEW [dbo].vw_Test AS ///* Let's test use of single quote in block comments. //*/ SELECT 1 + 1 AS SimpleSum; GO PRINT 'View vw_Test created'

CREATE VIEW [dbo] .vw_Test AS /// *让我们在块注释中测试单引号的使用。 // * / SELECT 1 + 1 AS SimpleSum; GO PRINT'查看vw_Test已创建'

Now, try compiling the view e.g. by clicking the F5 key. You will get an error message similar to this.

现在,尝试编译视图,例如单击F5键。您将收到与此类似的错误消息。

Connecting to ... Msg 102, Level 15, State 1, Procedure vw_Test, Line 16 Incorrect syntax near 'GO'. Disconnecting connection from ...

连接到...消息102,级别15,状态1,过程vw_Test,行16'GO'附近的语法不正确。断开与...的连接

If you remove the single quote by changing: Let's test to Lets test

如果您通过更改删除单引号:让我们进行测试以进行Lets测试

..you get a healthy message:

..你得到一个健康的信息:

Connecting to ... View vw_Test created Disconnecting connection from ...

正在连接...查看vw_Test已创建断开与...的连接

When you are using sqlcmd to run multiple external sql files, you may also encounter the same problem i.e. the separate files compile without problem in SSMS but when invoked from sqlcmd...it complains.

当您使用sqlcmd运行多个外部sql文件时,您可能也会遇到相同的问题,即SSMS中的单独文件编译没有问题,但是当从sqlcmd调用时...它会抱怨。