SQL 对 xml 的查询存储为 nvarchar(max)
SQL query on xml stored as nvarchar(max)
我一直在尝试完成此查询,但似乎找不到针对我的问题的答案。围绕我的问题有很多信息,但我找不到似乎可以回答它的 post。我敢肯定这很简单,但我就是做不对。这是我拥有的:
我有一个列将 XML 数据存储为 nvarchar(max)
,我试图查询标签但运气不佳。查看图片,<VU>
是根,我试图获取 <CPort>
(80) 的值,但我无法获得正确的语法。为了更清楚起见,我附上了屏幕截图。
到目前为止我拥有的是:
SELECT CAST(directory.dbo.unit.data AS XML) AS info
FROM Directory.dbo.unit
这让我可以将我的专栏投射到 XML,但我无法查询我新投射的专栏。
我试过:
SELECT CAST(directory.dbo.unit.data AS XML).query('(/VU/)') AS info
FROM Directory.dbo.unit
但这结果是 NULL
。我也试过:
SELECT CAST(directory.dbo.unit.data AS XML).value('(/VU/CPort)[1]', 'nvarchar(max)') AS info
FROM Directory.dbo.unit
这也会导致 NULL
我只是不知道我做错了什么。我在正确的轨道上吗?正在铸造查询这个的最佳方式。我尝试了子字符串,但这也没有用。我非常感谢大家对此的帮助。如果您需要更多信息或有更多问题,请告诉我。
尝试这样的事情...
declare @data table (text nvarchar(max) null);
insert @data values
('<VU><CPort>80</CPort></VU>'),
('<VU><CPort>81</CPort></VU>'),
('<VU><CPort>82</CPort></VU>'),
('<VU><CPort>83</CPort></VU>'),
('<VU><CPort>84</CPort></VU>'),
('<VU><CPort>85</CPort></VU>'),
('<VU><CPort>86</CPort></VU>'),
('<VU><CPort>87</CPort></VU>'),
('<VU><CPort>88</CPort></VU>'),
('<VU><CPort>89</CPort></VU>')
;
-- *** This is the important part ***
select cast(text as xml).value('(/VU/CPort)[1]', 'int') from @data;
我想对大家的帮助和指导表示感谢。我终于能够得到我想要的东西。我不得不转换包含 xml 存储为 nvarchar(max) 的数据的几列,然后绑定一些额外的列,但我认为这会起作用。
下面是我的最终解决方案。
With test as (Select Guid as guid, FirmwareVersion as FV, CAST(Directory.dbo.Unit.data as xml) as new, CAST(Directory.dbo.Unit.Location as xml) as loc from Directory.dbo.Unit)
select UnitName1, Manufacturer, model, FV, IP, TimeZone, PhysicalName, [user], CameraName, LogicalID
from (
select distinct
test.guid
,ue.name UnitName1
,test.new.value('(./VU/Mfct)[1]','varchar(100)') as Manufacturer
,test.new.value('(./VU/Model)[1]','varchar(100)') as Model
,test.new.value('(./VU/CAddr)[1]','varchar(100)') as IP
,test.loc.value('(./EntityLocation/TimeZoneId)[1]','varchar(100)') as TimeZone
,d.physicalname
,test.FV
,test.new.value('(./VU/User)[1]','varchar(100)') as [User]
,CE.NAME CameraName
,CE.LogicalID
from test
inner join Directory.dbo.Device d on test.guid = d.unit
INNER JOIN Directory.dbo.Entity UE ON test.Guid = uE.Guid
INNER JOIN Directory.dbo.Stream S ON S.Device = D.GUID
INNER JOIN Directory.dbo.Camera C ON S.Camera = C.GUID
INNER JOIN Directory.dbo.Entity CE ON C.Guid = CE.Guid
where charindex('Camera',d.PhysicalName,0) > 0
) x
order by IP desc, guid, UnitName1, cameraname, LogicalID
这可能不是最漂亮或最有效的写法,但它适合我!再次感谢大家! techstudent01
我一直在尝试完成此查询,但似乎找不到针对我的问题的答案。围绕我的问题有很多信息,但我找不到似乎可以回答它的 post。我敢肯定这很简单,但我就是做不对。这是我拥有的:
我有一个列将 XML 数据存储为 nvarchar(max)
,我试图查询标签但运气不佳。查看图片,<VU>
是根,我试图获取 <CPort>
(80) 的值,但我无法获得正确的语法。为了更清楚起见,我附上了屏幕截图。
到目前为止我拥有的是:
SELECT CAST(directory.dbo.unit.data AS XML) AS info
FROM Directory.dbo.unit
这让我可以将我的专栏投射到 XML,但我无法查询我新投射的专栏。
我试过:
SELECT CAST(directory.dbo.unit.data AS XML).query('(/VU/)') AS info
FROM Directory.dbo.unit
但这结果是 NULL
。我也试过:
SELECT CAST(directory.dbo.unit.data AS XML).value('(/VU/CPort)[1]', 'nvarchar(max)') AS info
FROM Directory.dbo.unit
这也会导致 NULL
我只是不知道我做错了什么。我在正确的轨道上吗?正在铸造查询这个的最佳方式。我尝试了子字符串,但这也没有用。我非常感谢大家对此的帮助。如果您需要更多信息或有更多问题,请告诉我。
尝试这样的事情...
declare @data table (text nvarchar(max) null);
insert @data values
('<VU><CPort>80</CPort></VU>'),
('<VU><CPort>81</CPort></VU>'),
('<VU><CPort>82</CPort></VU>'),
('<VU><CPort>83</CPort></VU>'),
('<VU><CPort>84</CPort></VU>'),
('<VU><CPort>85</CPort></VU>'),
('<VU><CPort>86</CPort></VU>'),
('<VU><CPort>87</CPort></VU>'),
('<VU><CPort>88</CPort></VU>'),
('<VU><CPort>89</CPort></VU>')
;
-- *** This is the important part ***
select cast(text as xml).value('(/VU/CPort)[1]', 'int') from @data;
我想对大家的帮助和指导表示感谢。我终于能够得到我想要的东西。我不得不转换包含 xml 存储为 nvarchar(max) 的数据的几列,然后绑定一些额外的列,但我认为这会起作用。 下面是我的最终解决方案。
With test as (Select Guid as guid, FirmwareVersion as FV, CAST(Directory.dbo.Unit.data as xml) as new, CAST(Directory.dbo.Unit.Location as xml) as loc from Directory.dbo.Unit)
select UnitName1, Manufacturer, model, FV, IP, TimeZone, PhysicalName, [user], CameraName, LogicalID
from (
select distinct
test.guid
,ue.name UnitName1
,test.new.value('(./VU/Mfct)[1]','varchar(100)') as Manufacturer
,test.new.value('(./VU/Model)[1]','varchar(100)') as Model
,test.new.value('(./VU/CAddr)[1]','varchar(100)') as IP
,test.loc.value('(./EntityLocation/TimeZoneId)[1]','varchar(100)') as TimeZone
,d.physicalname
,test.FV
,test.new.value('(./VU/User)[1]','varchar(100)') as [User]
,CE.NAME CameraName
,CE.LogicalID
from test
inner join Directory.dbo.Device d on test.guid = d.unit
INNER JOIN Directory.dbo.Entity UE ON test.Guid = uE.Guid
INNER JOIN Directory.dbo.Stream S ON S.Device = D.GUID
INNER JOIN Directory.dbo.Camera C ON S.Camera = C.GUID
INNER JOIN Directory.dbo.Entity CE ON C.Guid = CE.Guid
where charindex('Camera',d.PhysicalName,0) > 0
) x
order by IP desc, guid, UnitName1, cameraname, LogicalID
这可能不是最漂亮或最有效的写法,但它适合我!再次感谢大家! techstudent01