TABLE1
call_ID PARAM_CT status
------- -------- -------------
C001 3 NULL
TABLE2
+---------+---------+----------+----------+------------------+---------+
| seq_ID | REQ_ID | CALL_ID | SELL_ID | REGION | NUMBER|
+---------+---------+----------+----------+------------------+---------+
| 1 | 123 | C001 | S1 | ABC | 510563 |
| 2 | 123 | C001 | S1 | EFG | 122967 |
| 3 | 123 | C001 | S1 | HIJ | 614106 |
| 4 | 123 | C001 | S2 | ABC | 510563 |
| 5 | 123 | C001 | S2 | ERG | 122967 |
| 6 | 123 | C001 | S2 | HIJ | 614106 |
+---------+---------+----------+----------+------------------+---------+
下面是我用来查找源S1和S2之间行数差异的代码。请让我知道是否可以优化。
DECLARE vPARAM_CT VARCHAR(10) ;
DECLARE vSOURCE1 VARCHAR(10) ;
DECLARE vSOURCE2 VARCHAR(10) ;
BEGIN
SELECT PARAM_CT into vPARAM_CT FROM table1 WHERE call_id='C001' ;
SELECT COUNT(*) INTO vSOURCE1 FROM table2 WHERE call_id='C001' AND SOURCE='S1';
SELECT COUNT(*) INTO vSOURCE2 FROM table2 WHERE call_id='C001' AND SOURCE='S2';
IF (vPARAM_CT=vSOURCE1 AND vSOURCE2=vPARAM_CT ) THEN
BEGIN
UPDATE table1 SET RUN_STATUS='COMPLETED' WHERE call_id='C001' ;
END;
ELSE
BEGIN
UPDATE table1 SET RUN_STATUS='COMPLETE WITH MISSING COUNTRY CODE' WHERE call_id='C001' ;
END;
END IF;
END;
答案 0 :(得分:3)
首先,您只需要为一个PLSQL代码块编写一次declare
,然后返回
count
聚合为数字类型参数的结果。
然后,将您的select
语句合并为一个,并为if
块,如下所示:
DECLARE
v_cnt pls_integer;
v_call_id varchar2(10) := 'C001';
BEGIN
select count(*)
into v_cnt
from
( select call_id,
sum(case when source = 'S1' then 1 else 0 end ) as s1,
sum(case when source = 'S2' then 1 else 0 end ) as s2
from table2
group by call_id
) t2
join table1 t1 on t1.param_ct = t2.s1 and t1.param_ct = t2.s2
where call_id = 'C001';
IF ( v_cnt > 0 ) THEN
UPDATE table1 SET RUN_STATUS='COMPLETED' WHERE call_id=v_call_id ;
ELSE
UPDATE table1 SET RUN_STATUS='COMPLETED WITH MISSING COUNTRY CODE' WHERE call_id=v_call_id ;
END IF;
END;