问题描述
在Oracle DB(12)中,我有2个表:
表:STEP_DETAILS
+-----------+---------+---------------+
| record_id | step_id | material_type |
+===========+=========+===============+
| 1 | 1 | in |
+-----------+---------+---------------+
| 2 | 1 | in |
+-----------+---------+---------------+
| 3 | 1 | out |
+-----------+---------+---------------+
| 4 | 2 | in |
+-----------+---------+---------------+
| 5 | 2 | out |
+-----------+---------+---------------+
| 6 | 2 | out |
+-----------+---------+---------------+
表:ACTIONS_DETAILS
+-----------+-----------+---------------+
| record_id | action_id | material_type |
+===========+===========+===============+
| 1 | 11 | in |
+-----------+-----------+---------------+
| 2 | 11 | out |
+-----------+-----------+---------------+
| 3 | 12 | in |
+-----------+-----------+---------------+
| 4 | 12 | out |
+-----------+-----------+---------------+
所有id列均为INTEGER类型。
我需要计算两个表的输入材料。 在PL / SQL块中,我具有以下功能,每个功能都具有“几乎”相同的查询:
--count from step_details:
FUNCTION get_step_input_count(p_step_id step_details.step_id%TYPE)
RETURN INTEGER
IS
l_count INTEGER := 0;
BEGIN
SELECT COUNT(1)
INTO l_count
FROM step_details
WHERE step_id = p_step_id
AND material_type = 'in';
RETURN l_count;
END get_step_input_count;
--count from action_details:
FUNCTION get_action_input_count(p_action_id action_details.action_id%TYPE)
RETURN INTEGER
IS
l_count INTEGER := 0;
BEGIN
SELECT COUNT(1)
INTO l_count
FROM action_details
WHERE action_id = p_action_id
AND material_type = 'in';
RETURN l_count;
END get_action_input_count;
有可能编写一个单一的SELECT语句,该语句可以根据某种条件每次查询2个表之一,因此我最终将编写一个使用一个查询而不是2个函数的函数,例如:
FUNCTION get_input_count(p_parent_id integer,p_from varchar2)
RETURN INTEGER
IS
l_count INTEGER := 0;
BEGIN
SELECT COUNT(1)
INTO l_count
FROM (when p_from = 'S' then 'step_details'
when p_from = 'A' then 'action_details')
WHERE (when p_from = 'S' then 'step_id = p_parent_id'
when p_from = 'A' then 'action_id = p_parent_id')
AND material_type = 'in';
RETURN l_count;
END get_input_count;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)