我在表中有一组实体,如下所示:
PK Entity_Name Multiplier Min_Period Max_Period
01 No Mult 100 0 12
02 5% Mult 105 12 36
03 10% Mult 115 36 100
我想检查对它们的输入,并根据此输入所属的周期返回乘数。目前,我这样做的方式是如此令人费解,我确信必须是一种更简单的方法。我目前的方法是创建一个记录和一个游标:
TYPE r_multipliers IS RECORD (
entity_name VARCHAR2(100),
multiplier NUMBER(10),
min_period NUMBER(10),
max_period NUMBER(10)
);
TYPE t_multipliers IS TABLE OF r_multipliers
INDEX BY VARCHAR2(100);
my_rec r_multipliers;
l_multipliers t_multipliers;
CURSOR c_multipliers
IS
SELECT entity_name, multiplier, min_period, max_period
FROM multipliers;
然后我遍历游标,以存储每个可能的实体:
FOR record IN c_multipliers
LOOP
my_rec.entity_name := record.entity_name;
my_rec.multiplier := record.multiplier;
my_rec.min_period := record.min_period;
my_rec.max_period := record.max_period;
l_multipliers(record.entity_name) := my_rec;
END LOOP;
最后,我在查询中使用此逻辑,如下所示:
SELECT CASE
WHEN [input] BETWEEN l_multipliers('No Mult').min_period AND l_multipliers('No Mult').max_period
THEN l_multipliers('No Mult').multiplier
WHEN [input] BETWEEN l_multipliers('5% Mult').min_period AND l_multipliers('5% Mult').max_period
THEN l_multipliers('5% Mult').multiplier
//etc (up to six or seven of these case statements)
现在,所以这里使用了很多代码来做一些显然非常简单的事情。我对PL / SQL非常缺乏经验。我假设我大量过度复杂化事物。任何人都可以建议如何简化和缩短这个?
答案 0 :(得分:3)
这是一个简单的select语句,不需要PL / SQL:
select multiplier
from multipliers
where input between min_period and max_period;
答案 1 :(得分:0)
declare
l_mult multipliers.entity_name%type;
input_period multipliers.max_period%type;
begin
select multiplier into l_mult
from multipliers
where input_period >= min_period
and input_period < max_period;
-- return l_mult;
exception
when no_data_found then
-- do something
when others then
-- do something else
end;