解析 sql CAST xml

parsing sql CAST xml

我有一个 table 和一个 xml 列。 我想为 xml 中的特定键解析它,我通过执行以下操作来完成:

SELECT TOP 1000 
 CAST([PlatformInfo] as xml).value('(/PlatformInfo/@Device)[1]', 'varchar(max)')  as Device ,
 CAST([PlatformInfo] as xml).value('(/PlatformInfo/@SDKv)[1]', 'varchar(max)')  as SDKv 
FROM [myDB].[dbo].[HISTORY_TB]

现在我想对它进行排序,但总是出错。 如何对创建为 Device 和 SDKv 的自定义列进行排序? 我如何按特定值对其进行排序?

谢谢

Create table #companyname(name varchar(max))
Insert into #companyname (name) values
('<name>US Concrete Inc</name>'),
('<name>Digitiliti Inc</name>'),
('<name>Printron Inc</name>'),
('<name>Wesco Financial, LLC</name>'),
('<name>Fusion Restaurant Group Inc</name>')

select * from (
select top 4 CAST(name as xml).value('(/name)[1]','nvarchar(max)') as t 
from #companyname )tbl
order by tbl.t

同样:

 Select * from (
    SELECT TOP 1000 
CAST([PlatformInfo] as xml).value('(/PlatformInfo/@Device)[1]', 'varchar(max)')  as Device ,
CAST([PlatformInfo] as xml).value('(/PlatformInfo/@SDKv)[1]', 'varchar(max)')  as SDKv 
FROM [myDB].[dbo].[HISTORY_TB]) dertbl
order by dertbl.Device,dertbl.SDKv