我正在尝试获取XML标记,以显示是否存在任何值。 XML标记在XML模式文件中是必需的。但在某些情况下,此元素(标记)的值不存在。实际的Oracle11g SQL语句如下(但非数据库值为9999999999)。此Oracle XML SQL查询嵌入在其他XML SQL中。
如果事先感谢
,我将如何让XML标签显示如下所示<ProdId>
<idType></idType>
<id></id>
</ProdId>
SELECT XMLELEMENT ("prodId",
XMLELEMENT ("idType",NVL(x.idType,'N/A')),
XMLELEMENT ("id",NVL(x.id,'N/A'))
)
from
(SELECT DECODE (alternative_creation_ids.gc_alt_usage_id_type_code,'PROD',1,
'ISAN',2,
'CUI',3,9) AS oderby,
DECODE(alternative_creation_ids.gc_alt_usage_id_type_code,'PROD','SOCIETY-ID',
'ISAN','ISAN',
'CUI','CLOCKID','N/A') AS idType,
alternative_creation_ids.alternative_creation_id AS id,
alternative_creation_ids.cre_surr_id AS cre_surr_id
FROM alternative_creation_ids
WHERE alternative_creation_ids.gc_alt_usage_id_type_table = 'AUIT'
AND alternative_creation_ids.gc_alt_usage_id_type_code in ('ISAN','CUI','PROD')
order by 1) x
WHERE x.cre_surr_id = 9999999999999
表结构如下。
CREATE TABLE ALTERNATIVE_CREATION_IDS
(
ALTERNATIVE_CREATION_ID VARCHAR2(100 BYTE) NOT NULL,
CRE_SURR_ID NUMBER(12) NOT NULL,
TP_SURR_ID NUMBER(12),
GC_ALT_USAGE_ID_TYPE_TABLE VARCHAR2(5 BYTE) DEFAULT 'AUIT' NOT NULL,
GC_ALT_USAGE_ID_TYPE_CODE VARCHAR2(5 BYTE) NOT NULL,
SOC_SURR_ID NUMBER(12),
SURR_ID NUMBER(12) NOT NULL)
以及样本数据如下。
--SQL Statement which produced this data:
--
-- select * from alternative_creation_ids where surr_id in (5068066723,
-- 5068066724,
-- 5068066725,
-- 5068066726,
-- 5068066727,
-- 5068066729,
-- 5068066731,
-- 5068066732,
-- 5068066733);
--
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('4YvUOe8q9N2kQ5aflrezTs', 4890969424, 9513, 'AUIT', 'PROD',
NULL, 5068066723);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5GgVV6DFbWofT50C0dMRl0', 4891029626, 9513, 'AUIT', 'PROD',
NULL, 5068066724);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('4YyWUP4TOLM0lLKgbxXens', 4890969425, 9513, 'AUIT', 'PROD',
NULL, 5068066725);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5Gh2ldlMcEjcxGXQZO2erc', 4891029627, 9513, 'AUIT', 'PROD',
NULL, 5068066726);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5GhltgxuEfhzPiBxOoMKdl', 4891029628, 9513, 'AUIT', 'PROD',
NULL, 5068066727);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('3JfOCUI9q8yb3lJdEJPPbE', 4890969284, 9513, 'AUIT', 'PROD',
NULL, 5068066729);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5GjwMbQWxB97ddPnqDAUt8', 4891029629, 9513, 'AUIT', 'PROD',
NULL, 5068066731);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5GjLSJ14WJlRSUe4MNoNLE', 4891029630, 9513, 'AUIT', 'PROD',
NULL, 5068066732);
Insert into ALTERNATIVE_CREATION_IDS
(ALTERNATIVE_CREATION_ID, CRE_SURR_ID, TP_SURR_ID, GC_ALT_USAGE_ID_TYPE_TABLE, GC_ALT_USAGE_ID_TYPE_CODE,
SOC_SURR_ID, SURR_ID)
Values
('5GjSKGaNJzhVlL9ygkyNbY', 4891029631, 9513, 'AUIT', 'PROD',
NULL, 5068066733);
COMMIT;
正确答案是Alex Poole。仅供参考,我在XML SQL中使用了以下内容。我试图让标签只显示一个空白值,但出于什么原因它只显示了结束标记(见下文)。
<ProdId>
</Idtype>
</id>
</ProdId>
所以我去了
<ProdId>
<Idtype>N/A</Idtype>
<id>N/A</id>
</ProdId>
,( -- start level 5 tag prodId
SELECT XMLELEMENT ("prodId",
XMLELEMENT ("idType",x.idType),
XMLELEMENT ("idType",x.id))
from
(SELECT DECODE (alternative_creation_ids.gc_alt_usage_id_type_code,'PROD',1,
'ISAN',2,
'CUI',3,9) AS oderby,
DECODE (alternative_creation_ids.gc_alt_usage_id_type_code,'PROD','SOCIETY-ID',
'ISAN','ISAN',
'CUI','CLOCKID','N/A') AS idType,
alternative_creation_ids.alternative_creation_id AS id,
alternative_creation_ids.cre_surr_id AS cre_surr_id
FROM alternative_creation_ids
WHERE alternative_creation_ids.gc_alt_usage_id_type_table = 'AUIT'
AND alternative_creation_ids.gc_alt_usage_id_type_code in ('ISAN','CUI','PROD')
order by 1) x
WHERE x.cre_surr_id = cre.surr_id
UNION ALL
SELECT XMLELEMENT ("prodId",
XMLELEMENT ("idType",'N/A'),
XMLELEMENT ("id",'N/A')
)
FROM dual
WHERE NOT EXISTS (SELECT null FROM alternative_creation_ids
WHERE cre_surr_id = cre.surr_id)
) -- end level 5 tag prodId
答案 0 :(得分:1)
如果表中没有数据,则现有查询无法生成空标记。您可以添加一个联合来生成它们,检查ID是否存在:
...
WHERE x.cre_surr_id = 9999999999999
UNION ALL
SELECT XMLELEMENT ("prodId",
XMLELEMENT ("idType",null),
XMLELEMENT ("id",null)
)
FROM dual
WHERE NOT EXISTS (
SELECT null FROM alternative_creation_ids WHERE cre_surr_id = 9999999999999
);
XMLELEMENT("PRODID",XMLELEMENT("IDTYPE",NVL(X.IDTYPE,'N/A')),XMLELEMENT("ID",NVL
--------------------------------------------------------------------------------
<prodId><idType></idType><id></id></prodId>
如果你将过滤器提升为内联视图,你也可以在你的内联视图中进行联合(并将order-by子句移动到结尾;将它放在子查询中是没有意义的):
SELECT XMLELEMENT ("prodId",
XMLELEMENT ("idType",NVL(x.idType,'N/A')),
XMLELEMENT ("id",NVL(x.id,'N/A'))
)
FROM
(SELECT DECODE (alternative_creation_ids.gc_alt_usage_id_type_code,'PROD',1,
'ISAN',2,
'CUI',3,9) AS oderby,
DECODE(alternative_creation_ids.gc_alt_usage_id_type_code,'PROD','SOCIETY-ID',
'ISAN','ISAN',
'CUI','CLOCKID','N/A') AS idType,
alternative_creation_ids.alternative_creation_id AS id,
alternative_creation_ids.cre_surr_id AS cre_surr_id
FROM alternative_creation_ids
WHERE alternative_creation_ids.gc_alt_usage_id_type_table = 'AUIT'
AND alternative_creation_ids.gc_alt_usage_id_type_code in ('ISAN','CUI','PROD')
AND cre_surr_id = 9999999999999
UNION ALL
SELECT null, null, null, null
FROM dual
WHERE NOT EXISTS (
SELECT null FROM alternative_creation_ids WHERE cre_surr_id = 9999999999999)
) x
ORDER BY oderBy
/
XMLELEMENT("PRODID",XMLELEMENT("IDTYPE",NVL(X.IDTYPE,'N/A')),XMLELEMENT("ID",NVL
--------------------------------------------------------------------------------
<prodId><idType>N/A</idType><id>N/A</id></prodId>
...但是,它会选择你不想要的NVL处理。