仅当为输入参数找到特定元数据时,才会在游标中加入 Oracle JOIN
Oracle JOIN in cursor only if a certain metadata is found for input parameter
我有一个像这样连接的游标:
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我想修改游标并仅在元数据 table 中找到 p_input_id 时再添加 3 个连接。所以我想做这样的事情:
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
CASE
WHEN EXISTS (SELECT 1
FROM METADATA_TABLE
WHERE INPUT_ID = p_input_id
AND FLAG = 'Y')
THEN
JOIN table_3 c
ON c.ID_2 = b.ID_2
JOIN table_4 d
ON d.ID_3 = c.ID_3
END
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我怎样才能做到这一点?我可以考虑使用 LEFT JOIN 而不是 case/join。这有什么问题吗?
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
LEFT JOIN table_3 c
ON c.ID_2 = b.ID_2
LEFT JOIN table_4 d
ON d.ID_3 = c.ID_3
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我试图避免破坏正在使用它的 p_input_id 的当前光标。将有新的 p_input_id 需要使用额外的 table 连接。
您可以将 LEFT JOIN
用于 table_3
和 table_4
以及 count
在 metadata
table 中,如下所示:
SELECT * FROM TABLE_1 A
JOIN TABLE_2 B ON B.ID_1 = A.ID_1
JOIN ( SELECT COUNT(1) AS CNT
FROM METADATA_TABLE
WHERE INPUT_ID = P_INPUT_ID
AND FLAG = 'Y') MT
LEFT JOIN TABLE_3 C ON C.ID_2 = B.ID_2
AND MT.CNT > 0
JOIN TABLE_4 D ON D.ID_3 = C.ID_3
AND MT.CNT > 0
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = P_INPUT_ID;
COUNT
用于metadata
table保证returns只有一条记录。(替换exists
)
我有一个像这样连接的游标:
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我想修改游标并仅在元数据 table 中找到 p_input_id 时再添加 3 个连接。所以我想做这样的事情:
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
CASE
WHEN EXISTS (SELECT 1
FROM METADATA_TABLE
WHERE INPUT_ID = p_input_id
AND FLAG = 'Y')
THEN
JOIN table_3 c
ON c.ID_2 = b.ID_2
JOIN table_4 d
ON d.ID_3 = c.ID_3
END
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我怎样才能做到这一点?我可以考虑使用 LEFT JOIN 而不是 case/join。这有什么问题吗?
CURSOR C1 IS
SELECT *
FROM table_1 a
JOIN table_2 b
ON b.ID_1 = a.ID_1
LEFT JOIN table_3 c
ON c.ID_2 = b.ID_2
LEFT JOIN table_4 d
ON d.ID_3 = c.ID_3
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = p_input_id;
我试图避免破坏正在使用它的 p_input_id 的当前光标。将有新的 p_input_id 需要使用额外的 table 连接。
您可以将 LEFT JOIN
用于 table_3
和 table_4
以及 count
在 metadata
table 中,如下所示:
SELECT * FROM TABLE_1 A
JOIN TABLE_2 B ON B.ID_1 = A.ID_1
JOIN ( SELECT COUNT(1) AS CNT
FROM METADATA_TABLE
WHERE INPUT_ID = P_INPUT_ID
AND FLAG = 'Y') MT
LEFT JOIN TABLE_3 C ON C.ID_2 = B.ID_2
AND MT.CNT > 0
JOIN TABLE_4 D ON D.ID_3 = C.ID_3
AND MT.CNT > 0
WHERE TABLE_ID IS NOT NULL
AND TABLE_ID = P_INPUT_ID;
COUNT
用于metadata
table保证returns只有一条记录。(替换exists
)