2017-03-25 29 views
-2

我安装了SQL Server 2012。有一个名为channel_group的表格,其中有多个记录。如何从列SQL Server分析xml

[dbo].[CHANNEL_GROUP] 
(
    [ID] [nvarchar](255) NOT NULL, 
    [NAME] [nvarchar](255) NOT NULL, 
    [REVISION] [int] NULL, 
    [CHANNEL_GROUP] [nvarchar](max) NULL 
) 

CHANNEL_GROUP专栏中,我需要得到的ID标签的所有值与数据库中的每个记录每个通道标记每个记录。

什么是解析这个或查询我可以运行的最好方法是动态的?

<channelGroup version="3.4.2"> 
     <id>990b417d-27ae-4928-b4cc-cc010665615e</id> 
     <name>Production</name> 
     <revision>3</revision> 
     <lastModified> 
      <time>1490385251147</time> 
      <timezone>America/New_York</timezone> 
     </lastModified> 
     <description></description> 
     <channels> 
      <channel version="3.4.2"> 
       <id>321439ff-46d6-4c3d-b1cd-ebc48d3c7fd2</id> 
       <enabled>false</enabled> 
       <revision>0</revision> 
      </channel> 
      <channel version="3.4.2"> 
       <id>c2d06aee-3031-4c6a-a3c1-23f7e96c971c</id> 
       <enabled>false</enabled> 
       <revision>0</revision> 
      </channel> 
     </channels> 
</channelGroup> 
+1

使用XML方法https://docs.microsoft.com/en-us/sql/t-sql/xml/xml-data-type-方法 – Serg

+1

如果'CHANNEL_GROUP'列包含XML数据,那么您应该使用'XML'数据类型来声明它,而不是nvarchar ... – har07

回答

0
Declare @YourTable table (ID int,CHANNEL_GROUP varchar(max)) 
Insert Into @YourTable values 
(1,'<channelGroup version="3.4.2"><id>990b417d-27ae-4928-b4cc-cc010665615e</id><name>Production</name><revision>3</revision><lastModified><time>1490385251147</time><timezone>America/New_York</timezone></lastModified><description></description><channels><channel version="3.4.2"><id>321439ff-46d6-4c3d-b1cd-ebc48d3c7fd2</id><enabled>false</enabled><revision>0</revision></channel><channel version="3.4.2"><id>c2d06aee-3031-4c6a-a3c1-23f7e96c971c</id><enabled>false</enabled><revision>0</revision></channel></channels></channelGroup>'), 
(2,'<channelGroup version="3.9.9"><id>990b417d-27ae-4928-b4cc-cc010665615e</id><name>Production</name><revision>3</revision><lastModified><time>1490385251147</time><timezone>America/New_York</timezone></lastModified><description></description><channels><channel version="3.4.2"><id>SomeOtherID</id><enabled>false</enabled><revision>0</revision></channel><channel version="3.4.2"><id>AnotherID</id><enabled>false</enabled><revision>0</revision></channel></channels></channelGroup>') 

Select A.ID 
     ,Channel_ID= C.ID 
From @YourTable A 
Cross Apply (Select XMLData=cast(A.CHANNEL_GROUP as xml)) B 
Cross Apply (
       Select [id]  = f.n.value('(id)[1]','varchar(50)') 
       From B.XMLData.nodes('channelGroup/channels') t(n) 
       Cross Apply t.n.nodes('channel') f(n) 
      ) C 

返回

ID Channel_ID 
1 321439ff-46d6-4c3d-b1cd-ebc48d3c7fd2 
1 c2d06aee-3031-4c6a-a3c1-23f7e96c971c 
2 SomeOtherID 
2 AnotherID 
+0

该表尽管包含多行。我需要通过查询或其他方法来获取行。这似乎是硬编码其中一个表的值。基本上我会假设我需要查询1表,看看有多少行,然后解析每一个得到最终的记录列表。 – Villumanati

+0

好吧,例如,如果我有1000行在表中。我不会硬编码他们,因为它是解析它们。我不知道表中有多少行,但它需要动态。基本上我有CHANNEL_GROUP表中的4列(id,name,revision,channel_Group)。总的来说,我需要的是获取位于(ChannelGroup标记中的id标记)中的通道id。此id将是结果列让我们称之为channelgroupID,每个channelgroupID将在其中具有1个或多个channelID。必须将ChannelID将来自它似乎你已经解决了这个问题 – Villumanati

+0

ID \t NAME 29f37017-65d9-4052-954e-c3ca9f07136b \t开发 967a1deb-8b47-4eb0-865a-adf6998667bd \t组1 990b417d-27ae- XML结构4928-b4cc-cc010665615e \t生产 – Villumanati