导致插入错误的选择性Xml索引:字符串或二进制数据将被截断.语句已终止



我收到错误:

字符串或二进制数据将被截断。语句已终止。

XElement数据从C#通过Linq插入SQL Server 2012 Xml列导致错误。

在运行Profiler和其他工具后,我发现XML中的一个属性导致了错误:

我在C#代码中使用了这个:

XmlDocument.SchemaVersion = System.Reflection.Assembly.GetAssembly(typeof(Data.Schema.Document)).GetName().Version.ToString();

这应该会给出类似的输出

<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevNumber="1">
...
...
</tst:Root>

但它导致了错误被抛出,但这是:

如果我的测试像,SchemaVersion="3"SchemaVersion"0.2.123.1"可以正常工作

XmlDocument.SchemaVersion = "0.2.123.1"

SQL Server中的XML列上没有用于强制执行该错误的SchemaCollection。

SQL Server是否以某种方式强制SchemaVersion属性为特定长度而不由用户定义,或者为什么我使用的C#语句会导致错误?

编辑:

添加了在手动测试时失败并工作的示例Sql

失败:

Insert into XmlTable(XmlId, XmlColumn)
Values('1', '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.12345" RevisionNumber="1" />')

作品:

Insert into XmlTable(XmlId, XmlColumn)
Values('1', '<tst:Root xmlns:tst="http://tempuri.org/some.xsd" SchemaVersion="0.2.1234.1" RevisionNumber="1" />')

Selective Xml Index中指定的数据类型导致错误:

string or binary data would be truncated. the statement has been terminated.

我会发布索引,这样如果其他人有问题,也许会有所帮助。

根据指定的长度检查长度并更改它为我解决了问题。

CREATE SELECTIVE XML INDEX [xsi_XmlTable] ON [dbo].[XmlTable]
(
    [XmlColumn]
)
WITH XMLNAMESPACES
(
DEFAULT 'http://tempuri.org/some.xsd'
)
FOR
(
[1] = '/Root' as XQUERY 'node()', 
[2] = '/Root/@SchemaVersion' as SQL [nvarchar](10) SINGLETON 
)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)
GO

最新更新