如果查询包含记录,则根据查询创建 xml
Create xml from query if query contains records
我正在尝试根据 Oracle SQL 中的查询创建一些 xml。如果您进行 1 对 1 转换,这将非常容易,但是我需要围绕 returned xml 创建一个分组元素。这是我的工作代码。
WITH table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id))))
from table2;
问题是在这种情况下它 return 是一个空的测试元素,即使没有数据。我需要它 return NULL。到目前为止,我已经尝试了几件事。例如这种情况:
WITH table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN exists(select id from table2) THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
然而这给出了一个错误:
not a single-group group function
有谁知道我可以这样做的好方法吗?我正在使用 Oracle DB 12c。
根据您的描述,您可以将 exists
检查更改为 count
:
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
使用另一个 CTE 进行示例数据演示,没有匹配项:
set null "(null)"
WITH table1(id, code) as (
select 42, 'ignore' from dual),
table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
CASEWHENCOUNT(ID)>0THENXMLELEMENT("MES:TESTS",XMLATTRIBUTES('HTTP://TESTNAMESPAC
--------------------------------------------------------------------------------
(null)
并匹配:
WITH table1(id, code) as (
select 42, 'somevalue' from dual union all
select 43, 'somevalue' from dual),
table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
CASEWHENCOUNT(ID)>0THENXMLELEMENT("MES:TESTS",XMLATTRIBUTES('HTTP://TESTNAMESPAC
--------------------------------------------------------------------------------
<mes:tests xmlns:tns="http://testnamespace.com/"><tns:test><tns:ID>42</tns:ID></
tns:test><tns:test><tns:ID>43</tns:ID></tns:test></mes:tests>
我正在尝试根据 Oracle SQL 中的查询创建一些 xml。如果您进行 1 对 1 转换,这将非常容易,但是我需要围绕 returned xml 创建一个分组元素。这是我的工作代码。
WITH table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id))))
from table2;
问题是在这种情况下它 return 是一个空的测试元素,即使没有数据。我需要它 return NULL。到目前为止,我已经尝试了几件事。例如这种情况:
WITH table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN exists(select id from table2) THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
然而这给出了一个错误:
not a single-group group function
有谁知道我可以这样做的好方法吗?我正在使用 Oracle DB 12c。
根据您的描述,您可以将 exists
检查更改为 count
:
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
使用另一个 CTE 进行示例数据演示,没有匹配项:
set null "(null)"
WITH table1(id, code) as (
select 42, 'ignore' from dual),
table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
CASEWHENCOUNT(ID)>0THENXMLELEMENT("MES:TESTS",XMLATTRIBUTES('HTTP://TESTNAMESPAC
--------------------------------------------------------------------------------
(null)
并匹配:
WITH table1(id, code) as (
select 42, 'somevalue' from dual union all
select 43, 'somevalue' from dual),
table2 AS (
SELECT t.id
FROM table1 t
WHERE t.code = 'somevalue')
select CASE WHEN count(id) > 0 THEN
xmlelement("mes:tests",
xmlattributes('http://testnamespace.com/' as "xmlns:tns"),
xmlagg(xmlelement("tns:test",
xmlelement("tns:ID", id)))) ELSE null end
from table2;
CASEWHENCOUNT(ID)>0THENXMLELEMENT("MES:TESTS",XMLATTRIBUTES('HTTP://TESTNAMESPAC
--------------------------------------------------------------------------------
<mes:tests xmlns:tns="http://testnamespace.com/"><tns:test><tns:ID>42</tns:ID></
tns:test><tns:test><tns:ID>43</tns:ID></tns:test></mes:tests>