2015-10-20 44 views
0

我是PL/SQL的新手,並且今天正在處理遊標,並獲得了一個場景,我需要根據日期+金額+ cust_id獲取重複的交易類型+ txn_tpye。一旦我得到重複我不得不使用另一個遊標或使用選擇列值(日期+金額+ cust_id + txn_tpye)作爲where子句的正常循環。如何在遊標循環中使用Trunc(mydate)

在此之前,我只是試圖打印他們,如果我得到一個值或不,當我試圖打印mydate值出錯。請求你們的幫助。

SET SERVEROUTPUT ON 

declare 

CURSOR dup_check 

IS 

SELECT cust_id,amount,trunc(mydate),transaction_type,COUNT(1) 
FROM table_X WHERE trunc(mydate)>='10-OCT-2015' 
GROUP BY cust_id,amount,trunc(mydate),transaction_type 
HAVING COUNT(1)>1 ; 

BEGIN 

FOR UP_REC IN dup_check 

LOOP 

DBMS_OUTPUT.put_line(UP_REC.cust_id||' '||UP_REC.amount||UP_REC.trnasaction_type||**trunc(mydate))**; 

END LOOP; 

END; 


**PLS-00302: component 'mydate' must be declared** 

回答

0

附加別名如下TRUNC(指明MyDate)領域,並把UP_REC.mydate在DBMS_OUTPUT

SET SERVEROUTPUT ON 
declare 
CURSOR dup_check 
IS 
SELECT cust_id, 
     amount, 
     trunc(mydate) mydate, /* add an alias here */ 
     transaction_type, 
     COUNT(1) 
FROM table_X WHERE trunc(mydate) >= '10-OCT-2015' 
GROUP BY cust_id,amount,trunc(mydate),transaction_type HAVING COUNT(1)>1 ; 

BEGIN 

FOR UP_REC IN dup_check 
LOOP 
DBMS_OUTPUT.put_line(UP_REC.cust_id||' '||UP_REC.amount||UP_REC.trnasaction_type||UP_REC.mydate)); 
END LOOP; 

END; 
+0

納倫德拉您好,感謝您的信息。太好了!有效。簡單的事情讓生活更輕鬆。 – Selvan