2017-03-13 53 views
0

我想在選擇查詢中使用遊標中的c_msisdn值,但它給出沒有找到數據的錯誤,但是如果我自己提供值像WHERE MSISDN ='315XXX' WHERE MSISDN = c_msisdn結果返回。如何在select中使用遊標值。如何在選擇查詢中使用當前遊標值與在過程中

DECLARE 
c_msisdn SSM_SMSCDATA.MSISDN%type; 
c_status SSM_SMSCDATA.STATUS%type; 
c_promo_id SSM_SMSCDATA.PROMO_ID%type; 
view_all_status char(50) := ''; 
unsub_all_status char(50) := ''; 
services_subscribed char(400) := ''; 
CURSOR c_smscdata is SELECT MSISDN, STATUS, PROMO_ID FROM SSM_SMSCDATA; 

BEGIN 
OPEN c_smscdata; 
LOOP 
FETCH c_smscdata into c_msisdn, c_status, c_promo_id; 

SELECT SUBSCRIPTION_ID into services_subscribed FROM (SELECT LISTAGG(SUBSCRIPTION_ID, '-') WITHIN GROUP (ORDER BY MSISDN) AS SUBSCRIPTION_ID 
FROM SINGLESUBSCRIPTION 
WHERE MSISDN = c_msisdn 
GROUP BY MSISDN); 

IF c_promo_id = '2' THEN 
    view_all_status := 'View All'; 
    ELSE view_all_status := 'Unsub All'; 
END IF; 

IF c_status = 3 THEN 
    unsub_all_status := 'View Successful'; 
    ELSE unsub_all_status := 'Unsuccessful'; 
END IF; 


INSERT INTO SSM_DAILY_REPORT (msisdn,view_all,unsub_all,SERVICES) 
VALUES (c_msisdn,view_all_status,unsub_all_status,services_subscribed); 

--dbms_output.put_line(c_msisdn || ' ' || c_status || ' ' || c_promo_id); 

EXIT 
WHEN c_smscdata%notfound; 
END LOOP; 
CLOSE c_smscdata; 

END; 

回答

1

爲什麼你得到NO_DATA_FOUND例外,最可能的原因是因爲你在你的ssm_smscdata表中不存在在你的singlesubscription表MSISDN值。

如果我是你,我不打擾使用遊標循環。相反,我會做這一切在一個單一的INSERT語句,就像這樣:

INSERT INTO ssm_daily_report (msisdn, view_all, unsub_all, services) 
SELECT scs.msisdn, 
     CASE WHEN scs.promo_id = '2' THEN 'View All'; 
      ELSE 'Unsub All' 
     END view_all_status, 
     CASE WHEN scs.status = 3 THEN 'View Successful'; 
      ELSE 'Unsuccessful' 
     END unsub_all_status, 
     sss.services_subscribed 
FROM ssmsmscdata scs 
     INNER JOIN (SELECT msisdn, 
          LISTAGG(subscription_id, '-') WITHIN GROUP (ORDER BY msisdn) AS services_subscribed 
        FROM singlesubscription 
        GROUP BY msisdn) sss ON sss.msisdn = scs.msisdn; 

這樣,您避免重複的嵌套循環連接(釋放甲骨文選擇加入它認爲是最好用的類型,這可能是嵌套循環也可能不是)。您還可以避免您的光標循環涉及的所有上下文切換和逐行處理,並且您可以讓Oracle一舉完成所有工作。

而且,通過執行內部聯接,您避免出現在ssm_smscdata表,但不是singlesubscription,這些行不會返回行的棘手問題。如果您還需要返回這些行,則需要將上述查詢中的INNER JOIN轉換爲OUTER JOIN