SQLCMD 在插入操作时出现“在第 xxxxx 行附近的命令’&’处语法错误”的错误。

huangapple go评论65阅读模式
英文:

SQLCMD fails on INSERT with "Syntax error at line xxxxx near command '&'"

问题

我使用SQLCMD导入由mssql-scripter生成的DDL,并出现了以下错误:

SQLCMD 在插入操作时出现“在第 xxxxx 行附近的命令’&’处语法错误”的错误。

引发错误的INSERT语句如下:

INSERT [dbo].[Settings_ContentPartFieldDefinitionRecord] ([Id], [Name], [Settings], [ContentFieldDefinitionRecord_id], [ContentPartDefinitionRecord_Id]) VALUES (12, N'JavascripttoRun', N'以下为正确的插入语句,如果我在SSMS或Azure Data Studio中运行它,它可以正常工作。

我在互联网上找到了一些类似的错误消息文章,但没有适用于我的解决方法。

有什么想法吗?

英文:

I use SQLCMD to import DDL generated by mssql-scripter and i got this error:

SQLCMD 在插入操作时出现“在第 xxxxx 行附近的命令’&’处语法错误”的错误。

The offending INSERT is the following:

> INSERT [dbo].[Settings_ContentPartFieldDefinitionRecord] ([Id], [Name], [Settings], [ContentFieldDefinitionRecord_id], [ContentPartDefinitionRecord_Id]) VALUES (12, N'JavascripttoRun', N'<settings DisplayName="Javascript to Run" InsertStuffFieldSettings.RawHtml="<script type="text/javascript">
 $(document).ready(function() {
 ShowAdvancedLinkSettings();
 });
 $("#AdvancedLink_LinkType_Value").change(function() {
 ShowAdvancedLinkSettings();
 });
 
 function ShowAdvancedLinkSettings() {
 switch($("#AdvancedLink_LinkType_Value").val()) {
 case "External":
 $(".content-picker-field[data-field-name=''InternalContent'']").hide()
 $("#AdvancedLink_Caption_Text").parent().hide();
 $("#AdvancedLink_ExternalUrl_Value").parent().parent().show();
 $("#AdvancedLink_UrlBancaDati_Value").parent().parent().hide();
 break;
 case "Internal":
 $("#AdvancedLink_ExternalUrl_Value").parent().parent().hide();
 $(".content-picker-field[data-field-name=''InternalContent'']").show()
 $("#AdvancedLink_Caption_Text").parent().show();
 $("#AdvancedLink_UrlBancaDati_Value").parent().parent().hide();
 break;
 case "Banca dati":
 $("#AdvancedLink_ExternalUrl_Value").parent().parent().hide();
 $(".content-picker-field[data-field-name=''InternalContent'']").hide()
 $("#AdvancedLink_Caption_Text").parent().hide();
 $("#AdvancedLink_UrlBancaDati_Value").parent().parent().show();
 break;
 default:
 $("#AdvancedLink_ExternalUrl_Value").parent().parent().hide();
 $(".content-picker-field[data-field-name=''InternalContent'']").hide()
 $("#AdvancedLink_Caption_Text").parent().hide();
 $("#AdvancedLink_UrlBancaDati_Value").parent().parent().hide();
 break;
 }
 }
</script>

" InsertStuffFieldSettings.OnFooter="true" />', 7, 51)

The INSERT is correct, if i run it with SSMS or Azure Data Studio it works.

I found on internet various articles with similar error messages but no solutions that worked for me.

Any ideas?

答案1

得分: 1

SQLCMD具有一种变量机制,格式为$(variable_name),可以在运行时替换。

我的数据不应该是这些变量,会导致机制受骗并引发错误。

解决方案是使用-x选项,该选项会抑制变量替换机制(请参考https://learn.microsoft.com/it-it/sql/tools/sqlcmd/sqlcmd-utility?view=sql-server-ver16以供参考)。

英文:

It come out that SQLCMD has a mechanism of variables with the format $(variable_name) that can be replaced at run time .

My data are not meant to be such variables, with the effect of fooling the mechanism and cause the error.

The solution is to use the -x option which suppresses the variable substitution mechanism (for reference see https://learn.microsoft.com/it-it/sql/tools/sqlcmd/sqlcmd-utility?view=sql-server-ver16).

huangapple
  • 本文由 发表于 2023年7月4日 23:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定