2014-04-01 68 views
1

我從來沒有在SQL Server 2008中使用XML,我需要提取的字段列表到一個變量表你怎麼辦呢?如何讀取SQL Server 2008中的XML列?

假設我有一個XMLMain表看起來像一些所謂的XMLDATA列像下面我怎麼提取SQL字段列表?

![在這裏輸入的形象描述] [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> 

我想提取重複字段作爲逗號像前分離的價值。在重複我們有三個值(嗨,你好,你怎麼樣?)

誰能請幫助我。?

+0

它會更容易幫助您查詢,如果你能後的實際XML,而不是XML的圖片。 –

+0

@MikaelEriksson我已經編輯我的問題,讓我的XML是對你可見。 – User5590

+0

謝謝,我已經添加了答案。 –

回答

1
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 

結果:

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

謝謝Mikael,這就是我一直在尋找的東西。再一次感謝你。 – User5590