1. 等值查找XML字段数据:
;WITH XMLNAMESPACES
('http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition' AS ns)
SELECT *
FROM #temp1
WHERE command.exist('(/ns:Report/ns:DataSets/ns:DataSet/ns:Query/ns:DataSourceName[.="Test1"])')=1
GO
2. 模糊查找XML字段数据:
;WITH XMLNAMESPACES
('http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition' AS ns)
SELECT *
FROM #temp1
WHERE command.exist('(/ns:Report/ns:DataSets/ns:DataSet/ns:Query/ns:CommandText[contains(.,"test_sp")])')=1
GO
3. 解析XML字段中弄个node数据:
SELECT command.value('
declare namespace ns="http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition";
(/ns:Report/ns:DataSets/ns:DataSet/ns:Query/ns:CommandText)[1] ', 'varchar(max)') as Result
FROM #temp1