create or replace procedure sp_bi_scsu412(P_CODE VARCHAR2,P_DATE DATE) is
l_scsu412_K1 SCSU412.SCSU412_K1%TYPE;
L_SCSU412_K2 SCSU412.SCSU412_K2%TYPE;
L_SCSU412_K3 SCSU412.SCSU412_K3%TYPE;
L_SCSU412_K4 SCSU412.SCSU412_K4%TYPE;
L_SCSU412_K5 SCSU412.SCSU412_K5%TYPE;
L_SCSU412_01 SCSU412.SCSU412_01%TYPE;
L_SCSU412_02 SCSU412.SCSU412_02%TYPE;
L_SCSU412_03 SCSU412.SCSU412_03%TYPE;
L_SCSU412_04 SCSU412.SCSU412_04%TYPE;
L_SCSU412_05 SCSU412.SCSU412_05%TYPE;
L_SCSU412_06 SCSU412.SCSU412_06%TYPE;
L_SCSU412_07 SCSU412.SCSU412_07%TYPE;
L_SCSU412_K6 SCSU412.SCSU412_K6%TYPE;
TYPE CUR IS REF CURSOR;
CURA CUR;
CURSOR CUR_1 IS
SELECT A.SCSU410_02, --分公司代号
A.SCSU410_03, --分公司名称
A.SCSU410_04, --本部代号
A.SCSU410_05, --本部名称
B.SCSU411_06, --门店代号
B.SCSU411_07, --门店名称
A.SCSU410_06, --发起人工号
A.SCSU410_07, --发起人名称
COUNT(A.SCSU410_01), --目标铺货门店数
A.SCSU410_01, --提报年月
B.SCSU411_08, --汇总机型
B.SCSU411_02 --虚拟客户代号
FROM CUX_K2_SCSU410 A, CUX_K2_SCSU411 B
WHERE A.PROCINSTID = B.PROCINSTID
AND B.SCSU411_08 = P_CODE
AND A.SCSU410_01 = TO_CHAR(P_DATE,'YYYYMM')
GROUP BY A.SCSU410_02,
A.SCSU410_03,
A.SCSU410_04,
A.SCSU410_05,
B.SCSU411_06,
B.SCSU411_07,
A.SCSU410_06,
A.SCSU410_07,
A.SCSU410_01,
B.SCSU411_08,
B.SCSU411_02,
B.SCSU411_06;
BEGIN
OPEN CUR_1;
LOOP
FETCH CUR_1
INTO l_scsu412_K1,
L_SCSU412_01,
L_SCSU412_K2,
L_SCSU412_02,
L_SCSU412_K3,
L_SCSU412_03,
L_SCSU412_K4,
L_SCSU412_04,
L_SCSU412_05,
L_SCSU412_07,
L_SCSU412_K5,
L_SCSU412_K6;
EXIT WHEN CUR_1%NOTFOUND;
OPEN CURA FOR SELECT TO_CHAR(L_SCSU412_07,'YYYYMM'),sum(SCSU071_07)
FROM SCSU070, SCSU071, SCSM130
WHERE SCSU070_K1 = SCSU071_K1
AND SCSU070_K2 = SCSU071_K2
AND SCSU070_06 = L_SCSU412_K6
AND SCSU070_07 = L_SCSU412_01
AND SCSU071_04 = SCSM130_01
AND SCSM130_K1 = L_SCSU412_K5;
IF L_SCSU412_06 = '' THEN
SELECT TO_CHAR(L_SCSU412_07,'YYYYMM'),sum(SCSU071_07)
FROM SCSU070, SCSU071, SAIM530
WHERE SCSU070_K1 = SCSU071_K1
AND SCSU070_K2 = SCSU071_K2
AND SCSU070_06 = L_SCSU412_K6
AND SCSU070_07 = L_SCSU412_01
AND SCSU071_04 = SAIM530_K2
AND SAIM530_K1 = L_SCSU412_K5;
END IF;
LOOP
FETCH CURA INTO c2rec;
EXIT WHEN CURA%NOTFOUND;
sql;
END LOOP;
CLOSE CURA;
BEGIN
DELETE FROM SCSU412;
INSERT INTO SCSU412
(scsu412_K1,
SCSU412_K2,
SCSU412_K3,
SCSU412_K4,
SCSU412_01,
SCSU412_02,
SCSU412_03,
SCSU412_04,
SCSU412_05,
SCSU412_06,
SCSU412_07,
SCSU412_K5,
SCSU412_K6)
VALUES
(l_scsu412_K1,
L_SCSU412_K2,
L_SCSU412_K3,
L_SCSU412_K4,
L_SCSU412_01,
L_SCSU412_02,
L_SCSU412_03,
L_SCSU412_04,
L_SCSU412_05,
L_SCSU412_06,
L_SCSU412_07,
L_SCSU412_K5,
L_SCSU412_K6);
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
COMMIT;
END LOOP;
end sp_bi_scsu412;
上面的过程应该不对,但基本能看出来逻辑是什么。求大神帮忙教一下 该怎么写
3 个解决方案
#1
可以不用游标,楼主看看merge into
#2
不需要:从第一个游标里取查到的数据,在第二个游标里做参数。
解决方法有两种:
1.创建临时表,把
FROM CUX_K2_SCSU410 A, CUX_K2_SCSU411 B
FROM SCSU070, SCSU071, SCSM130
FROM SCSU070, SCSU071, SAIM530
写到临时表中,然后关联查询。
2.使用merge into也可以处理。
解决方法有两种:
1.创建临时表,把
FROM CUX_K2_SCSU410 A, CUX_K2_SCSU411 B
FROM SCSU070, SCSU071, SCSM130
FROM SCSU070, SCSU071, SAIM530
写到临时表中,然后关联查询。
2.使用merge into也可以处理。
#3
推荐采用方法1。
采用merge into代码可读性会比较差,而且很容易出错。
采用merge into代码可读性会比较差,而且很容易出错。
#1
可以不用游标,楼主看看merge into
#2
不需要:从第一个游标里取查到的数据,在第二个游标里做参数。
解决方法有两种:
1.创建临时表,把
FROM CUX_K2_SCSU410 A, CUX_K2_SCSU411 B
FROM SCSU070, SCSU071, SCSM130
FROM SCSU070, SCSU071, SAIM530
写到临时表中,然后关联查询。
2.使用merge into也可以处理。
解决方法有两种:
1.创建临时表,把
FROM CUX_K2_SCSU410 A, CUX_K2_SCSU411 B
FROM SCSU070, SCSU071, SCSM130
FROM SCSU070, SCSU071, SAIM530
写到临时表中,然后关联查询。
2.使用merge into也可以处理。
#3
推荐采用方法1。
采用merge into代码可读性会比较差,而且很容易出错。
采用merge into代码可读性会比较差,而且很容易出错。