问题描述
我有一个场景。如果用户传递参数Entity_type
,则仅需要插入该entity
(table
)的数据。如果他没有传递任何参数,那么您需要将所有必需表的数据插入一个表中。
因此,我们有一个表DYNAMICENTITYGTT
,它将从Item
表中获取数据(如果通过),从org
表中获取数据。但是,如果proc中的Entity_type
参数为null,则会从两个表中获取数据。
还将根据UPDATE_MODE
的类型存储另一列,即添加或删除。
目标表是相同的。源表及其列名不同,但类型相同。
我已经按照以下步骤编写了相同的程序。
无论如何,我只是要求那里有更好的代码。我的意思是可以用更聪明的方式写吗?因为我要重复多行。我给出了2个实体的示例,但是有7个实体,因此代码将非常庞大。
CREATE OR REPLACE procedure UPDATE_DYNAMIC_ENTITY(ENTITY_TYPE varchar2 default null,UPDATE_MODE varchar2)
Is
x number;
BEGIN
IF UPPER(entity_type)='ITEM' then
if upper(UPDATE_MODE)='DELETE' then
INSERT INTO DYNAMICENTITYGTT(Entity_type,Entity_id,Entity_code,Synonyms,Action) select Entity_type,Item_id,item_name,item_desc,'delete' from ITEMDE;
ELSIF lower(UPDATE_MODE)='add' then
INSERT INTO DYNAMICENTITYGTT(Entity_type,'add' from ITEMDE;
END IF;
ELSIF UPPER(entity_type)='ORG' then
if upper(UPDATE_MODE)='DELETE' then
INSERT INTO DYNAMICENTITYGTT(Entity_type,ORG_id,org_name,org_desc,'delete' from ORGDE;
ELSIF lower(UPDATE_MODE)='add' then
INSERT INTO DYNAMICENTITYGTT(Entity_type,'add' from ORGDE;
END IF;
ELSE
if upper(UPDATE_MODE)='DELETE' then
INSERT INTO DYNAMICENTITYGTT(Entity_type,'delete' from ITEMDE;
INSERT INTO DYNAMICENTITYGTT(Entity_type,'add' from ITEMDE;
INSERT INTO DYNAMICENTITYGTT(Entity_type,'add' from ORGDE;
END IF;
END IF;
END UPDATE_DYNAMIC_ENTITY;
解决方法
基本上,我看到两个带有两个变量的插入来确定其内容。您是从select语句插入的,因此可以在条件不符合预期的情况下操纵这些选择以不返回任何值。
对于参数p_update_mode
来说很容易,如果包含值“ delete”,则插入“ delete”;如果包含值“ add”,则插入“ add”。
对于参数p_entity_type
,当其值为“ NULL”时,我们从两个选择中插入;如果值是“ ITEM”,则仅从itemde
表插入;如果值是“ ORG”。
现在,如果orgde
中存在一些无效值,则所有选择都不会产生数据,因为我们只能识别“ NULL”,“ ITEM”和“ ORG”。但是对于参数p_entity_type
,我们直接修改该值并将其用于insert中,因此最好检查一下输入值是否对我们有效。
p_update_mode
在撰写本文时,您有7个实体,因此这种方法将导致有7个插入,正如我认为的(如果我错了,请告诉我),每个实体在不同的表中都有自己的数据集。
也有可能将所有这些表连接起来,并使其成为单个插入,如下例所示,每个新实体都意味着仅将新选择添加到语句的CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,p_update_mode VARCHAR2) IS
BEGIN
IF lower(p_update_mode) NOT IN ('add','delete')
THEN
RAISE VALUE_ERROR; -- maybe use raise_application_error for more details about problem
END IF;
--
INSERT INTO dynamicentitygtt
(entity_type,entity_id,entity_code,synonyms,action)
SELECT upper(NVL(p_entity_type,'ITEM')),item_id,item_name,item_desc,lower(p_update_mode)
FROM itemde
WHERE upper(p_entity_type) = 'ITEM'
OR p_entity_type IS NULL;
--
INSERT INTO dynamicentitygtt
(entity_type,'ORG')),org_id,org_name,org_desc,lower(p_update_mode)
FROM orgde
WHERE upper(p_entity_type) = 'ORG'
OR p_entity_type IS NULL;
END update_dynamic_entity;
部分中。但是我不确定这种情况下的性能。那将取决于您的桌子有多满。
WITH
即使这对您造成麻烦,您也可以创建一个CREATE OR REPLACE PROCEDURE update_dynamic_entity(p_entity_type VARCHAR2 DEFAULT NULL,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
FROM itemde
UNION ALL
-- ORG table
SELECT 'ORG' entity_type,-- This separates inserted values
org_id,org_desc
FROM orgde
-- NEXT entity table
)
SELECT upper(entity_type),data_id,data_name,data_desc,lower(p_update_mode)
FROM data_view
WHERE upper(p_entity_type) = entity_type
OR p_entity_type IS NULL;
END update_dynamic_entity;
,在其中执行VIEW
,并从UNION
中删除该WITH
,并添加新的实体选择进入PROCEDURE
而不是VIEW
。