SQL Key/Value 对的 XQuery 选择器
SQL XQuery Selector for Key/Value Pairs
我在这样的专栏中有一些 xml。
<ScopedWorkflowConfiguration xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/workflow/2012/xaml/activities">
<appSettings>
<AppSetting>
<Key>CurrentWebUri</Key>
<Value>http://myurl.org</Value>
</AppSetting>
<AppSetting>
<Key>SomethingElse</Key>
<Value>1</Value>
</AppSetting>
<AppSetting>
<Key>AnotherThing</Key>
<Value>420</Value>
</AppSetting>
</appSettings>
</ScopedWorkflowConfiguration>
正在尝试 select Value 节点的值。
;WITH Casted AS (SELECT t.Id, t.[Configuration] As XmlData FROM dbo.MyTable AS t)
Select Distinct
Id
,pv.value('//*:Key="CurrentWebUri"/Value', 'nvarchar(max)') As CurrentWebUri
From
Casted
Cross Apply Casted.XmlData.nodes(N'//*:ScopedWorkflowConfiguration//*:appSettings//*:AppSetting') AS A(pv)
我知道我很接近——但我对这里的语法有疑问://:Key="CurrentWebUri"/Value*
上述查询导致此错误:
A node or set of nodes is required for /
你能帮忙吗?
添加一个结果集以供参考,以进一步阐明我所追求的:
+----+------------------+------------------+------------------+
| ID | CurrentWebUri | SomethingElse | AnotherThing |
+----+------------------+------------------+------------------+
| 1 | example.org | 1 | 420 |
+----+------------------+------------------+------------------+
| 2 | example.com | 6 | 29 |
+----+------------------+------------------+------------------+
缺少默认命名空间声明,XPath 表达式已关闭。随着对所需输出的最新澄清,需要动态 SQL PIVOTing。
SQL
-- DDL and sample data population, start
USE tempdb;
DROP TABLE IF EXISTS dbo.tbl;
CREATE TABLE dbo.tbl (ID INT IDENTITY PRIMARY KEY, [Configuration] xml);
INSERT INTO dbo.tbl ([Configuration])
VALUES ('<ScopedWorkflowConfiguration xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/workflow/2012/xaml/activities">
<appSettings>
<AppSetting>
<Key>CurrentWebUri</Key>
<Value>http://myurl.org</Value>
</AppSetting>
<AppSetting>
<Key>SomethingElse</Key>
<Value>1</Value>
</AppSetting>
<AppSetting>
<Key>AnotherThing</Key>
<Value>420</Value>
</AppSetting>
</appSettings>
</ScopedWorkflowConfiguration>');
-- DDL and sample data population, end
DECLARE @columns AS NVARCHAR(MAX),
@SQL AS NVARCHAR(MAX);
;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/workflow/2012/xaml/activities')
SELECT TOP(1) @columns = STUFF([Configuration].query('for $s in /ScopedWorkflowConfiguration/appSettings/AppSetting/Key
return <x>{concat(",",$s)}</x>')
.value('.','varchar(max)'),1,1,'')
FROM dbo.tbl;
SET @SQL =
N';WITH XMLNAMESPACES (DEFAULT ''http://schemas.microsoft.com/workflow/2012/xaml/activities''), rs AS
(
SELECT ID
, c.value(''(Key/text())[1]'', ''NVARCHAR(MAX)'') AS [Key]
, c.value(''(Value/text())[1]'', ''NVARCHAR(MAX)'') AS [Value]
FROM dbo.tbl as tbl
CROSS APPLY tbl.[Configuration].nodes(''/ScopedWorkflowConfiguration/appSettings/AppSetting'') AS t(c)
)
SELECT ID,' + @columns + N' FROM rs
PIVOT (
MAX([Value])
FOR [Key] IN (' + @columns + N')
) AS pvt;
';
EXEC sp_executesql @SQL;
Output
+----+------------------+---------------+--------------+
| ID | CurrentWebUri | SomethingElse | AnotherThing |
+----+------------------+---------------+--------------+
| 1 | http://myurl.org | 1 | 420 |
+----+------------------+---------------+--------------+
我在这样的专栏中有一些 xml。
<ScopedWorkflowConfiguration xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/workflow/2012/xaml/activities">
<appSettings>
<AppSetting>
<Key>CurrentWebUri</Key>
<Value>http://myurl.org</Value>
</AppSetting>
<AppSetting>
<Key>SomethingElse</Key>
<Value>1</Value>
</AppSetting>
<AppSetting>
<Key>AnotherThing</Key>
<Value>420</Value>
</AppSetting>
</appSettings>
</ScopedWorkflowConfiguration>
正在尝试 select Value 节点的值。
;WITH Casted AS (SELECT t.Id, t.[Configuration] As XmlData FROM dbo.MyTable AS t)
Select Distinct
Id
,pv.value('//*:Key="CurrentWebUri"/Value', 'nvarchar(max)') As CurrentWebUri
From
Casted
Cross Apply Casted.XmlData.nodes(N'//*:ScopedWorkflowConfiguration//*:appSettings//*:AppSetting') AS A(pv)
我知道我很接近——但我对这里的语法有疑问://:Key="CurrentWebUri"/Value*
上述查询导致此错误:
A node or set of nodes is required for /
你能帮忙吗?
添加一个结果集以供参考,以进一步阐明我所追求的:
+----+------------------+------------------+------------------+
| ID | CurrentWebUri | SomethingElse | AnotherThing |
+----+------------------+------------------+------------------+
| 1 | example.org | 1 | 420 |
+----+------------------+------------------+------------------+
| 2 | example.com | 6 | 29 |
+----+------------------+------------------+------------------+
缺少默认命名空间声明,XPath 表达式已关闭。随着对所需输出的最新澄清,需要动态 SQL PIVOTing。
SQL
-- DDL and sample data population, start
USE tempdb;
DROP TABLE IF EXISTS dbo.tbl;
CREATE TABLE dbo.tbl (ID INT IDENTITY PRIMARY KEY, [Configuration] xml);
INSERT INTO dbo.tbl ([Configuration])
VALUES ('<ScopedWorkflowConfiguration xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://schemas.microsoft.com/workflow/2012/xaml/activities">
<appSettings>
<AppSetting>
<Key>CurrentWebUri</Key>
<Value>http://myurl.org</Value>
</AppSetting>
<AppSetting>
<Key>SomethingElse</Key>
<Value>1</Value>
</AppSetting>
<AppSetting>
<Key>AnotherThing</Key>
<Value>420</Value>
</AppSetting>
</appSettings>
</ScopedWorkflowConfiguration>');
-- DDL and sample data population, end
DECLARE @columns AS NVARCHAR(MAX),
@SQL AS NVARCHAR(MAX);
;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/workflow/2012/xaml/activities')
SELECT TOP(1) @columns = STUFF([Configuration].query('for $s in /ScopedWorkflowConfiguration/appSettings/AppSetting/Key
return <x>{concat(",",$s)}</x>')
.value('.','varchar(max)'),1,1,'')
FROM dbo.tbl;
SET @SQL =
N';WITH XMLNAMESPACES (DEFAULT ''http://schemas.microsoft.com/workflow/2012/xaml/activities''), rs AS
(
SELECT ID
, c.value(''(Key/text())[1]'', ''NVARCHAR(MAX)'') AS [Key]
, c.value(''(Value/text())[1]'', ''NVARCHAR(MAX)'') AS [Value]
FROM dbo.tbl as tbl
CROSS APPLY tbl.[Configuration].nodes(''/ScopedWorkflowConfiguration/appSettings/AppSetting'') AS t(c)
)
SELECT ID,' + @columns + N' FROM rs
PIVOT (
MAX([Value])
FOR [Key] IN (' + @columns + N')
) AS pvt;
';
EXEC sp_executesql @SQL;
Output
+----+------------------+---------------+--------------+
| ID | CurrentWebUri | SomethingElse | AnotherThing |
+----+------------------+---------------+--------------+
| 1 | http://myurl.org | 1 | 420 |
+----+------------------+---------------+--------------+