2015-10-20 28 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