我安装了SQL Server 2012。有一个名为InnerTransId cText cVal
--------------- ----------- -------------------
89274503 Idea Date: 2017-03-22
89274503 Idea Name: The Invisible Cloak
的表在表中有多个记录。
u = User.first # name = bar
User.first.update(name: "foo")
u.name # => 'bar'
u2 = User.find(u.id)
u.name == u2.name # => false
从channel_group
列的每条记录中,我需要获取数据库中每条记录的每个通道标记的id标记中的所有值。
解析这个或我可以运行的查询的最佳方法是动态的吗?
[dbo].[CHANNEL_GROUP]
(
[ID] [nvarchar](255) NOT NULL,
[NAME] [nvarchar](255) NOT NULL,
[REVISION] [int] NULL,
[CHANNEL_GROUP] [nvarchar](max) NULL
)
答案 0 :(得分: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