`bcp in` 失败 "INSERT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'"

`bcp in` fails with "INSERT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'"

我用 bcp %database%.MyTable out MyTable.dmp -n -T -S %sqlserver% 导出了 table(使用 EF Core 2.2 创建)。

使用 bcp %database%.MyTable in MyTable.dmp -n -T -S %sqlserver% 重新导入它时出现此错误:

SQLState = 37000, NativeError = 1934
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]INSERT failed because the following SET options have incorrect settings: 'QUOTED_IDENTIFIER'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.

table 是用 QUOTED_IDENTIFIER = ON 创建的,不包含任何 computed column:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [MyTable](
    [Id] [bigint] IDENTITY(1,1) NOT NULL,
    [OwnerLoginId] [bigint] NOT NULL,
    [RowVersion] [timestamp] NULL,
    [CreatedAt] [datetime2](7) NOT NULL,
    [DataId] [bigint] NOT NULL,
    [SomeString] [nvarchar](30) NOT NULL,
    [AnotherString] [nvarchar](30) NULL,
 CONSTRAINT [PK_MyTable] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [MyTable]  WITH CHECK ADD  CONSTRAINT [FK_MyTable_SomeOtherTable_DataId_SomeString] FOREIGN KEY([DataId], [SomeString])
REFERENCES [SomeOtherTable] ([DataId], [SomeString])
GO

ALTER TABLE [MyTable] CHECK CONSTRAINT [FK_MyTable_SomeOtherTable_DataId_SomeString]
GO

ALTER TABLE [MyTable]  WITH CHECK ADD  CONSTRAINT [FK_MyTable_DataTable_DataId] FOREIGN KEY([DataId])
REFERENCES [DataTable] ([Id])
GO

ALTER TABLE [MyTable] CHECK CONSTRAINT [FK_MyTable_DataTable_DataId]
GO

为了向后兼容,BCP utilityQUOTED_IDENTIFIER OFF 相连。您需要添加 -q 选项才能使用 QUOTED_IDENTIFIER ON.

BCP 文档指示在指定 -q 选项时将整个限定的 table 名称括在引号中。但是,当数据库名称不符合 regular identifier naming rules 时,这不起作用。数据库名称中的句点是这里的罪魁祸首。

解决方法是指定一个由两部分组成的 table 名称并使用 -d 选项分别指定数据库名称:

bcp "MySchema.MyTable" in "MyTable.dmp" -q -n -T -S "(localdb)\MSSQLLocalDB" -d "My.Database"

恕我直言,最好按照正则标识符的规则来命名对象,避免像这样把名字围起来,绕圈圈。

错误消息表明您正在使用需要 QUOTED_IDENTIFIER ON 的计算列索引以外的其他功能。其中包括过滤索引、索引视图和 XML 索引。我的猜测是过滤索引或索引视图是这里的罪魁祸首。