使用多个 if 条件简化程序
Posted
技术标签:
【中文标题】使用多个 if 条件简化程序【英文标题】:Simplify procedure with multiple if condition 【发布时间】:2021-04-22 16:50:06 【问题描述】:我是新手,有一个需要简化的程序。 它有多个 if 和 inserts,我想简化它。
BEGIN
IF oldProductType IS NOT NULL AND PRODUCTTYPE != oldProductType THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Product Type', oldProductType, PRODUCTTYPE, '');
UPDATE ab_project SET product_type = PRODUCTTYPE where project_id = PROJECTID;
END IF;
IF oldProgramManager IS NOT NULL AND PROGRAMMANAGER != oldProgramManager THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Program Manager', oldProgramManager, PROGRAMMANAGER, '');
UPDATE ab_project SET program_manager = PROGRAMMANAGER where project_id = PROJECTID;
END IF;
IF oldProductManager IS NOT NULL AND PRODUCTMANAGER != oldProductManager THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Product Manager', oldProductManager, PRODUCTMANAGER, '');
UPDATE ab_project SET product_manager = PRODUCTMANAGER where project_id = PROJECTID;
END IF;
IF oldProductName IS NOT NULL AND PRODUCTNAME != oldProductName THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Product Name', oldProductName, PRODUCTNAME, '');
UPDATE ab_project SET product_name = PRODUCTNAME where project_id = PROJECTID;
END IF;
IF oldDescription IS NOT NULL AND PRODUCTDESCRIPTION != oldDescription THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Product Description', oldDescription, PRODUCTDESCRIPTION, '');
UPDATE ab_project SET product_description = PRODUCTDESCRIPTION where project_id = PROJECTID;
END IF;
IF oldRequestType IS NOT NULL AND REQUESTTYPE != oldRequestType THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Request Type', oldRequestType, REQUESTTYPE, '');
UPDATE ab_project SET request_type = REQUESTTYPE where project_id = PROJECTID;
END IF;
IF oldOtherProduct IS NOT NULL AND OTHERPRODUCT != oldOtherProduct THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Other Product', oldOtherProduct, OTHERPRODUCT, '');
UPDATE ab_project SET Other_Product = OTHERPRODUCT where project_id = PROJECTID;
END IF;
IF oldBusinessRationale IS NOT NULL AND BUSINEs-s-rATIONALE != oldBusinessRationale THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Business Rationale', oldBusinessRationale, BUSINEs-s-rATIONALE, '');
UPDATE ab_project SET business_rationale = BUSINEs-s-rATIONALE where project_id = PROJECTID;
END IF;
IF oldSoftwareDependency IS NOT NULL AND SOFTWAREDEPENDENCY != oldSoftwareDependency THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Software Dependency', oldSoftwareDependency, SOFTWAREDEPENDENCY, '');
UPDATE ab_project SET software_dependency = SOFTWAREDEPENDENCY where project_id = PROJECTID;
END IF;
IF OldOtherDependency IS NOT NULL AND OTHERDEPENDENCY != OldOtherDependency THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Other Dependency', OldOtherDependency, OTHERDEPENDENCY, '');
UPDATE ab_project SET other_dependency = OTHERDEPENDENCY where project_id = PROJECTID;
END IF;
IF OldReleaseCycleID IS NOT NULL AND RELEASECYCLEID != OldReleaseCycleID THEN
groupIdVal := prcChangeGroupIdSeq.NEXTVAL;
INSERT INTO CHANGE_GROUP VALUES(groupIdVal,projectid,user_id,SYSDATE);
insert into change_log (id, groupId, type, field, oldvalue, newvalue, fieldtypeid)
values (prcChangeLogIdSeq.NEXTVAL, groupIdVal, 'project', 'Release Cycle ID', OldReleaseCycleID, RELEASECYCLEID, '');
UPDATE ab_project SET release_cycle_id = RELEASECYCLEID where project_id = PROJECTID;
END IF;
commit;
exception
when others then
raise_application_error(-20001,'An error was encountered - '||SQLCODE||' - Error - '||SQLERRM);
end;
【问题讨论】:
这里的“优化”是什么意思?通常,这指的是性能,这意味着您在这里遇到性能问题。如果是这种情况,您对代码进行了哪些分析?各种语句的查询计划是什么?如果没有表和索引定义,看起来一切都在进行单行插入和更新操作,这应该很快。假设多个if
语句可以同时为真,您可以将单独的 ab_project
更新替换为单个更新,但这不应该是一个巨大的改进。
但也许你没有使用正常意义上的“优化”,你真的在问是否可以简化/重构过程?
是的,也许我没有使用正确的词。因为,我是新手,我想知道是否有更好的方法来编写这段代码
好的。那你能解释一下这个程序在做什么,为什么?例如,每个if
语句都对change_group
执行相同的插入操作。因此,如果 6 个 if
语句全部为真,那么您将在 change_group
中获得 6 个新行,除了序列生成的主键之外的相同数据。这似乎不太可能有用。我认为您需要一个 update
或 ab_project
,而不是在每个 if
语句中都有一个 update
,除非有某种原因您真的希望同一行可能被更新多次。
您的异常处理程序似乎没有做任何有用的事情,只是隐藏了有关实际发生错误的错误堆栈的信息。我会完全摆脱它,除非你能在你提出的异常中真正增加价值。
【参考方案1】:
CASE 可能就是你想要的
【讨论】:
以上是关于使用多个 if 条件简化程序的主要内容,如果未能解决你的问题,请参考以下文章