Error report:
ORA-00904: "RDD"."USED_END_DATE": invalid identifier
ORA-02063: preceding line from ADSWEBPROD
ORA-06512: at line 5
以下查询使我无效标识符错误。
DECLARE
TYPE demo_date_array_type IS TABLE OF dc_demo_dates.demo_date_id%TYPE;
updated_demo_dates demo_date_array_type;
BEGIN
SELECT ddd.demo_date_id
BULK COLLECT INTO
updated_demo_dates
FROM
dc_demo_dates ddd
JOIN
dc_demo_reservations ddr
ON
ddd.reservation_id = ddr.reservation_id
AND ddr.status NOT IN ('DENIED','CANCELLED')
AND ddr.demo_type <> 'Training'
AND ddr.environment IS NOT NULL
AND ddd.last_update_date > SYSTIMESTAMP - 1 -- updated in the last day
AND ddd.demo_start_date > SYSTIMESTAMP - 14 -- no older than 2 weeks
JOIN
(SELECT demo_date_id, used_start_date, used_end_date
FROM rps_by_demo_date
GROUP BY demo_date_id, used_start_date, used_end_date ) rdd
ON rdd.demo_date_id = ddd.demo_date_id
WHERE
ddd.demo_start_date != NVL(rdd.used_start_date, SYSTIMESTAMP) -- either start date
OR ddd.demo_end_date != NVL(rdd.used_end_date, SYSTIMESTAMP); -- or end date has changed
END;
如果我删除GROUP BY demo_date_id, used_start_date, used_end_date
或如果我更改OR
的最后AND
,则代码会成功执行(除非它不能提供我需要的结果)
你知道可能会发生什么吗? 谢谢
编辑: 我将group by子句从子查询移动到主查询中,并获得了我想要的结果。但我仍然不明白导致错误的原因。
答案 0 :(得分:0)
试试这个: -
DECLARE
TYPE demo_date_array_type IS TABLE OF dc_demo_dates.demo_date_id%TYPE;
updated_demo_dates demo_date_array_type;
BEGIN
SELECT ddd.demo_date_id
BULK COLLECT INTO
updated_demo_dates
FROM
dc_demo_dates ddd
JOIN
dc_demo_reservations ddr
ON
ddd.reservation_id = ddr.reservation_id
AND ddr.status NOT IN ('DENIED','CANCELLED')
AND ddr.demo_type <> 'Training'
AND ddr.environment IS NOT NULL
AND ddd.last_update_date > SYSTIMESTAMP - 1 -- updated in the last day
AND ddd.demo_start_date > SYSTIMESTAMP - 14 -- no older than 2 weeks
JOIN
(SELECT demo_date_id, used_start_date, used_end_date
FROM rps_by_demo_date
GROUP BY demo_date_id, used_start_date, used_end_date ) rdd
ON rdd.demo_date_id = ddd.demo_date_id
AND (ddd.demo_start_date != NVL(rdd.used_start_date, SYSTIMESTAMP)
OR ddd.demo_end_date != NVL(rdd.used_end_date, SYSTIMESTAMP));
END;
我已将where子句移动到连接条件。使用带有连接操作的where子句有时会导致意外结果。你的where子句可能无法识别rdd。但是,您的连接条件可能会识别它。