简体   繁体   English

如何在SQL Server 2008中读取XML列?

[英]How to read XML column in SQL Server 2008?

I have never used XML in SQL Server 2008, I need to extract a list of fields into a variable table how do you do it? 我从未在SQL Server 2008中使用过XML,我需要将字段列表提取到变量表中,您该怎么做?

Given that I have a column called xmldata in a XMLMain table that looks like something like below how do I extract the list of fields in sql? 假设我在XMLMain表中有一列名为xmldata的列,如下所示,该如何提取sql中的字段列表?

![enter image description here][1] ![在此处输入图片描述] [1]

<?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:SampleForm:-myXSD-2014-03-29T09-41-23" solutionVersion="1.0.0.18" productVersion="15.0.0.0" PIVersion="1.0.0.0" href="http://bipc20/sites/team-1303/FormServerTemplates/SampleForm.xsn"?>
<?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.4"?>
<my:myFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-03-29T09:41:23" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-US">
<my:field1>1</my:field1>
<my:field2>2</my:field2>
<my:field3>true</my:field3>
<my:field4 xsi:nil="true" />
<my:field5 xsi:nil="true" />
<my:field6>4</my:field6>
<my:FormName>2014-04-01T15:11:47</my:FormName>
<my:Repeating>hi</my:Repeating>
<my:Repeating xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-03-29T09:41:23">hello</my:Repeating>
<my:Repeating xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-03-29T09:41:23">how are  you?</my:Repeating>
</my:myFields>

I want to extract value of Repeating field as comma separated like for ex. 我想提取重复字段的值作为逗号分隔,如前。 in Repeating we have three values (hi,hello,how are you?) 在重复中,我们有三个值(嗨,你好,你好吗?)

can anyone please help me.? 谁能帮帮我吗。?

with xmlnamespaces('http://schemas.microsoft.com/office/infopath/2003/myXSD/2014-03-29T09:41:23' as my)
select M.XMLData.value('(/my:myFields/my:field1/text())[1]', 'int') as field1,
       M.XMLData.value('(/my:myFields/my:field2/text())[1]', 'int') as field2,
       M.XMLData.value('(/my:myFields/my:field3/text())[1]', 'bit') as field3,
       M.XMLData.value('(/my:myFields/my:FormName/text())[1]', 'datetime') as FormName,
       (
         select ','+R.X.value('text()[1]', 'nvarchar(max)')
         from M.XMLData.nodes('/my:myFields/my:Repeating') as R(X)
         for xml path(''), type
       ).value('substring(text()[1], 2)', 'nvarchar(max)') as Repeating
from XMLMain as M

Result: 结果:

field1      field2      field3 FormName                Repeating
----------- ----------- ------ ----------------------- -----------------------
1           2           1      2014-04-01 15:11:47.000 hi,hello,how are  you?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM