在SQL中从XML中选择属性

时间:2012-09-28 02:03:32

标签: sql xml string

好的,我的表有一个名为bdParameters的列,数据类型为text。存储在bdParameters列中的数据是XML保存为文本。

数据如下:

<root>
    <row statusID="ALL" subID="ALL" viewMaskFields="false" masID="219" 
         userID="13" prptyDate="08/19/2011" docID="21b15c9e-76a5-44ed-afbb-96b6df2cf881" 
         resmID="8806" leaID="6345" reshID="3560" doEmail="" emailFrom="" emailTo="" 
         emailMsg="penarands" emailSubject="" fieldList="1" fmtID="2" OptInCES="Yes" 
         SendVaiEmailCES="1" RPIPMCURL="blabla@blabla.com" />
    <Custom>
       <UpdateResidentLetter>
          <Row Status="NOTPRINTED" Notes="Created from bulk lettersinterface" />
       </UpdateResidentLetter>
    </Custom>
</root>

我希望能够从该xml中仅选择属性emailMsg

这可能吗?如果是这样,怎么样?

1 个答案:

答案 0 :(得分:0)

将xml.value与@符号一起使用以获取属性

declare @t table (col xml);
insert @t select '
<root><row statusID="ALL" subID="ALL" viewMaskFields="false" masID="219" 
userID="13" prptyDate="08/19/2011" docID="21b15c9e-76a5-44ed-afbb-96b6df2cf881" 
resmID="8806" leaID="6345" reshID="3560" doEmail="" emailFrom="" emailTo="" 
emailMsg="penarands" emailSubject="" fieldList="1" fmtID="2" OptInCES="Yes" 
SendVaiEmailCES="1" RPIPMCURL="lm-10-254-9-52.onesitedev.realpage.com" /><Custom>
<UpdateResidentLetter><Row Status="NOTPRINTED" Notes="Created from bulk letters
 interface" /></UpdateResidentLetter></Custom></root>';

select col.value('(/root/row/@emailMsg)[1]','varchar(max)') emailMsg
from @t

-- result
penarands