Sql 组合2 if块并创建单个查询
是否有可能合并if块并从下面创建单个块。两者在逻辑上是相连的 如果p_update_mode已满,则必须添加为p_entity_type传递的表的所有数据。如果它是增量的,那么我已经将连接条件添加到所选的添加。另外,如果p_entity_type为null,则必须为item表和org表添加数据Sql 组合2 if块并创建单个查询,sql,database,oracle,plsql,Sql,Database,Oracle,Plsql,是否有可能合并if块并从下面创建单个块。两者在逻辑上是相连的 如果p_update_mode已满,则必须添加为p_entity_type传递的表的所有数据。如果它是增量的,那么我已经将连接条件添加到所选的添加。另外,如果p_entity_type为null,则必须为item表和org表添加数据 CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,
CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,
p_update_mode VARCHAR2) IS
BEGIN
IF lower(p_update_mode) = 'incremental'
THEN
INSERT INTO dynamicentitygtt
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
( -- ITEM table
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
creation_date
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
creation_date
FROM orgde
-- NEXT entity table
)
SELECT upper(t.entity_type),
t.data_id,
t.data_name,
t.data_desc,
CASE
WHEN t.creation_date > b.max_last_update_date THEN
'update'
WHEN t.creation_date < b.max_last_update_date THEN
'add'
END
FROM data_view t
JOIN batch_run_details b
ON b.entity_type = t.entity_type
WHERE upper(p_entity_type) = t.entity_type
OR p_entity_type IS NULL;
END IF;
IF UPPER(UPDATE_MODE)='FULL' then
INSERT INTO dynamicentitygtt
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
(
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
'add' action
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
'add' action
FROM orgde
)
SELECT upper(entity_type), data_id, data_name, data_desc,action
FROM data_view
WHERE upper(p_entity_type) = entity_type
OR p_entity_type IS NULL;
END IF;
END update_dynamic_entity;
注意:这段代码未经测试,我可能在这里用逗号,或者在那里用括号 这两个块似乎只在action列和join中有所不同,因此可以消除2个IF分支,并将p_update_模式的检查移动到CASE语句中,如下所示:
CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,
p_update_mode VARCHAR2) IS
BEGIN
INSERT INTO dynamicentitygtt
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
( -- ITEM table
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
creation_date
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
creation_date
FROM orgde
-- NEXT entity table
)
SELECT upper(t.entity_type),
t.data_id,
t.data_name,
t.data_desc,
CASE lower(p_update_mode)
WHEN 'incremental' THEN
CASE
WHEN t.creation_date > b.max_last_update_date THEN
'update'
WHEN t.creation_date < b.max_last_update_date THEN
'add'
END
WHEN 'full' THEN
'add'
END action
FROM data_view t
LEFT JOIN batch_run_details b
ON b.entity_type = t.entity_type
AND lower(p_update_mode )='incremental'
WHERE (upper(p_entity_type) = t.entity_type OR p_entity_type IS NULL)
AND (lower(p_update_mode) = 'full'
OR (lower(p_update_mode) = 'incremental' AND b.entity_type IS NOT NULL)
);
END update_dynamic_entity;
您的完整块中的查询表示我们不应在此模式下加入B。因此,LEFT JOIN子句仅在增量模式下返回行,但在完全模式下不应生成行
这需要是一个左连接,否则我们可能无法从您的data_视图中获得任何与B中的实体不对应的行。换句话说,如果这仍然是一个常规联接,那么由于联接中的AND子句,整个查询在完整模式下将获得零行
最后,底部WHERE子句中的AND过滤器由于存在左连接而变得非常必要。如果没有这一点,在增量模式下运行时,无论B中是否有对应的实体行,都将获得data_视图中的每一行。即使您是在实体_id上进行连接,左连接也会为T中的每一行返回一行,即使B中没有匹配的行,因为左连接就是这样设计的
综上所述,您必须决定是否值得将这两个区块混合在一起。仅仅因为你可以,并不意味着你应该。如果让它运行一些测试,您的性能可能会更好。只有您知道数据量和处理频率。你还需要考虑你的代码的维护,因为下一个家伙/GAL必须弄清楚这里发生了什么。 < P>注意:这个代码是未经测试的,我可能在这里偏离逗号或括号……/P> 这两个块似乎只在action列和join中有所不同,因此可以消除2个IF分支,并将p_update_模式的检查移动到CASE语句中,如下所示:
CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,
p_update_mode VARCHAR2) IS
BEGIN
INSERT INTO dynamicentitygtt
(entity_type, entity_id, entity_code, synonyms, action)
WITH data_view AS
( -- ITEM table
SELECT 'ITEM' entity_type, -- This separates inserted values
item_id data_id,
item_name data_name,
item_desc data_desc,
creation_date
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type, -- This separates inserted values
org_id,
org_name,
org_desc,
creation_date
FROM orgde
-- NEXT entity table
)
SELECT upper(t.entity_type),
t.data_id,
t.data_name,
t.data_desc,
CASE lower(p_update_mode)
WHEN 'incremental' THEN
CASE
WHEN t.creation_date > b.max_last_update_date THEN
'update'
WHEN t.creation_date < b.max_last_update_date THEN
'add'
END
WHEN 'full' THEN
'add'
END action
FROM data_view t
LEFT JOIN batch_run_details b
ON b.entity_type = t.entity_type
AND lower(p_update_mode )='incremental'
WHERE (upper(p_entity_type) = t.entity_type OR p_entity_type IS NULL)
AND (lower(p_update_mode) = 'full'
OR (lower(p_update_mode) = 'incremental' AND b.entity_type IS NOT NULL)
);
END update_dynamic_entity;
您的完整块中的查询表示我们不应在此模式下加入B。因此,LEFT JOIN子句仅在增量模式下返回行,但在完全模式下不应生成行
这需要是一个左连接,否则我们可能无法从您的data_视图中获得任何与B中的实体不对应的行。换句话说,如果这仍然是一个常规联接,那么由于联接中的AND子句,整个查询在完整模式下将获得零行
最后,底部WHERE子句中的AND过滤器由于存在左连接而变得非常必要。如果没有这一点,在增量模式下运行时,无论B中是否有对应的实体行,都将获得data_视图中的每一行。即使您是在实体_id上进行连接,左连接也会为T中的每一行返回一行,即使B中没有匹配的行,因为左连接就是这样设计的
综上所述,您必须决定是否值得将这两个区块混合在一起。仅仅因为你可以,并不意味着你应该。如果让它运行一些测试,您的性能可能会更好。只有您知道数据量和处理频率。你还需要考虑你的代码的维护,因为下一个G/GAL必须弄清楚这里发生了什么。只是OP的一个注释,在他之前的一些问题中提到过。t.creation_date>b.max_last_update_date和t.creation_date