我的数据库需要在用户发送时存储xml请求,而xml有2个Communications元素, WP 和 SA ,但它们不是固定的(我的意思是位置) ):
<bms:OrgInfo>
<bms:Communications>
<bms:CommQualifier>WP</bms:CommQualifier>
<bms:CommPhone>5555551212</bms:CommPhone>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>SA</bms:CommQualifier>
<bms:Address>
<bms:Address1>1234 Test Avenue</bms:Address1>
<bms:Address2>1234 Test Avenue</bms:Address2>
<bms:City>Alamogordo</bms:City>
<bms:StateProvince>NM</bms:StateProvince>
<bms:PostalCode>88310</bms:PostalCode>
<bms:CountryCode>US</bms:CountryCode>
</bms:Address>
</bms:Communications>
</bms:OrgInfo>
有时 SA 元素将是第一个。
如何查询它而不用担心poistion
这是我当前的查询,我用index来查询它:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XmlStore]') AND type in (N'U'))
DROP TABLE [dbo].[XmlStore]
GO
CREATE TABLE [dbo].[XmlStore](
[XmlRequest] [xml] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?>
<bms:OrgInfo xmlns:bms="http://example.org">
<bms:Communications>
<bms:CommQualifier>WP</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>SA</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>EM</bms:CommQualifier>
</bms:Communications>
</bms:OrgInfo>')
;WITH xmlnamespaces ( 'http://example.org' AS bms )
Select
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[1]', 'nvarchar(100)') as WP_Value,
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[2]', 'nvarchar(100)') as SA_Value,
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications/bms:CommQualifier)[3]', 'nvarchar(100)') as EM_Value
FROM [XmlStore] AS t
DROP TABLE [dbo].[XmlStore]
如果用户重新订购通讯元素,那将是一场灾难!
答案 0 :(得分:4)
您可以像这里一样添加XQuery predicate
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XmlStore]') AND type in (N'U'))
DROP TABLE [dbo].[XmlStore]
GO
CREATE TABLE [dbo].[XmlStore](
[XmlRequest] [xml] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?>
<bms:OrgInfo xmlns:bms="http://example.org">
<bms:Communications>
<bms:CommQualifier>WP</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>SA</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>EM</bms:CommQualifier>
</bms:Communications>
</bms:OrgInfo>')
- 再次使用XML,但顺序不同
INSERT INTO [XmlStore]([XmlRequest]) VALUES('<?xml version="1.0" encoding="UTF-8"?>
<bms:OrgInfo xmlns:bms="http://example.org">
<bms:Communications>
<bms:CommQualifier>SA</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>EM</bms:CommQualifier>
</bms:Communications>
<bms:Communications>
<bms:CommQualifier>WP</bms:CommQualifier>
</bms:Communications>
</bms:OrgInfo>')
- 查询(所有索引均为[1]
)
;WITH xmlnamespaces ( 'http://example.org' AS bms )
Select
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="WP"]/bms:CommQualifier)[1]', 'nvarchar(100)') as WP_Value,
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="SA"]/bms:CommQualifier)[1]', 'nvarchar(100)') as SA_Value,
t.XmlRequest.value('(/bms:OrgInfo/bms:Communications[bms:CommQualifier="EM"]/bms:CommQualifier)[1]', 'nvarchar(100)') as EM_Value
FROM [XmlStore] AS t
DROP TABLE [dbo].[XmlStore]