[英]SQL : How can I extract substring what I want from a long xml string?
I have a column which includes an XML value in my SQL table.我的 SQL 表中有一个包含 XML 值的列。 I have to read the values in these three elements:
我必须读取这三个元素中的值:
<POSTN_ID>0000-0000H1-POS</POSTN_ID>
<ROLE_ID>0000-00002B-ROL</ROLE_ID>
<STATUS>1</STATUS>
Which SQL methods can help me in this situation?在这种情况下,哪些 SQL 方法可以帮助我?
<parameter name="OrgHierarchyDatasets_diffgram">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<PositionRoles diffgr:id="PositionRoles1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<POSTN_ID>0000-0000H1-POS</POSTN_ID>
<ROLE_ID>0000-00002B-ROL</ROLE_ID>
<STATUS>1</STATUS>
</PositionRoles>
</NewDataSet>
</diffgr:diffgram>
</parameter>
<parameter name="ExistNodeCheck" type="System.Boolean">True</parameter>
The following syntax can help you, capture the objects you define and take them to a table model.以下语法可以帮助您捕获您定义的对象并将它们带到表模型中。
DECLARE @xml xml = '
<parameter name="OrgHierarchyDatasets_diffgram">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<PositionRoles diffgr:id="PositionRoles1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<POSTN_ID>0000-0000H1-POS</POSTN_ID>
<ROLE_ID>0000-00002B-ROL</ROLE_ID>
<STATUS>1</STATUS>
</PositionRoles>
</NewDataSet>
</diffgr:diffgram>
</parameter>
<parameter name="ExistNodeCheck" type="System.Boolean">True</parameter>'
SELECT
Tbl.Col.value('POSTN_ID[1]', 'nvarchar(50)') AS POSTN_ID,
Tbl.Col.value('ROLE_ID[1]', 'nvarchar(50)') AS ROLE_ID,
Tbl.Col.value('STATUS[1]', 'nvarchar(50)') AS STATUS
FROM @xml.nodes('//PositionRoles') Tbl(Col)
Use xPath/xQuery:使用 xPath/xQuery:
select cast('<parameter name="OrgHierarchyDatasets_diffgram">
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<PositionRoles diffgr:id="PositionRoles1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<POSTN_ID>0000-0000H1-POS</POSTN_ID>
<ROLE_ID>0000-00002B-ROL</ROLE_ID>
<STATUS>1</STATUS>
</PositionRoles>
</NewDataSet>
</diffgr:diffgram>
</parameter>
<parameter name="ExistNodeCheck" type="System.Boolean">True</parameter>' as xml) as col
into #tab
go
select
col,
nod,
nod.query('./POSTN_ID/text()'),
nod.value('./ROLE_ID[1]','varchar(255)')
from (
select
*, col.query('//PositionRoles/node()') as nod
from #tab
) x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.