我有一个SQL表,其中包含一些包含一些HTML / XML标记的列。这是一个例子:
<root><Physicians><name>John Helsinki, MD</name>
<picture><img src="/uploadedImages/svr/physicians/images/John web.jpg?n=9059" alt="Helsinki, John Photo" /></picture>
<gender>M</gender>
<specialty><a href="/WA/test.aspx?56" title="Eye Care">OPT</a></specialty>
<specialty2></specialty2>
<specialty3></specialty3>
<additional_specialty></additional_specialty>
</Physicians>
</root>
如何提取<img>
之间的<picture></picture>
标记?
我知道我可以在SQL中使用LIKE
关键字,但我不确定如何继续...
答案 0 :(得分:2)
您可以使用XPath利用列类型 - XML
declare @thing XML
set @thing = '<root><Physicians><name>John Helsinki, MD</name>
<picture><img src="/uploadedImages/svr/physicians/images/John web.jpg?n=9059" alt="Helsinki, John Photo" /></picture>
<gender>M</gender>
<specialty><a href="/WA/test.aspx?56" title="Eye Care">OPT</a></specialty>
<specialty2></specialty2>
<specialty3></specialty3>
<additional_specialty></additional_specialty>
</Physicians>
</root>'
SELECT @thing.query('/root/Physicians/picture/img')
通过表格的另一种方式:
DECLARE @XmlTable TABLE (ID INT NOT NULL, XmlData XML)
INSERT INTO @XmlTable
(ID, XmlData)
VALUES
(1, '<root><Physicians><name>John Helsinki, MD</name>
<picture><img src="/uploadedImages/svr/physicians/images/John web.jpg?n=9059" alt="Helsinki, John Photo" /></picture>
<gender>M</gender>
<specialty><a href="/WA/test.aspx?56" title="Eye Care">OPT</a></specialty>
<specialty2></specialty2>
<specialty3></specialty3>
<additional_specialty></additional_specialty>
</Physicians>
</root>')
select XmlData.query('/root/Physicians/picture/img') from @XmlTable
这利用了任何XML变量类型上可用的query method,因此在此上下文中:
select [column].query('/root/Physicians/picture/img') from [table]