1
我的數據庫需要存儲的XML請求時,用戶發送以及XML有2個通信元件,WP和SA,但他們沒有固定的(我的意思是位置):MSSQL查詢XML差異爲了
<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
這裏是我當前的查詢,我使用的索引進行查詢:
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]
這將是一個災難,如果用戶重新排序通訊元素!
我們可以看到你的腳本嗎? – maSTAShuFu
@maSTAShuFu:是的,我編輯帖子更新腳本 –