Home » Developer & Programmer » Forms » Reduce the Code
Reduce the Code [message #625156] Thu, 02 October 2014 02:30 Go to next message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
Below I mentioned the code:
In that only for "T.CR_TYPE" column I repeat code.
The value for column "T.CR_TYPE" is either "C" or "R".

Anyone can guide me to reduce the repeating code.

BEGIN
		      SELECT T.UNIT_RATE 
		  		INTO   :TRANSACTIONS_MST.CAST_PRIZE
  	  		FROM   TRANSACTIONS_MST T
  	  		WHERE  T.EFF_DT    = :BLOCK_PARAM.EFF_DT
  	  		AND    T.GRADE_CD  = :TRANSACTIONS_MST.GRADE_CD 
  	  		AND	   T.CR_TYPE   = 'C';
	  	 EXCEPTION
		  		WHEN NO_DATA_FOUND THEN
			  		:TRANSACTIONS_MST.CAST_PRIZE := NULL;
	  	 END;
	
	     BEGIN
		  		SELECT T.UNIT_RATE 
		  		INTO   :TRANSACTIONS_MST.ROLL_PRIZE
		  		FROM   TRANSACTIONS_MST T
  	  		WHERE  T.EFF_DT    = :BLOCK_PARAM.EFF_DT
  	  		AND    T.GRADE_CD  = :TRANSACTIONS_MST.GRADE_CD 
  	  		AND	   T.CR_TYPE   = 'R';  	  
	  	 EXCEPTION
		  		WHEN NO_DATA_FOUND 
		  		THEN :TRANSACTIONS_MST.ROLL_PRIZE := NULL;
	  	 END;	  	 

Re: Reduce the Code [message #625157 is a reply to message #625156] Thu, 02 October 2014 02:56 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
You could declare an explicit cursor with a parameter of cr_type and use that but it won't save you much unless there are more selects like the above 2. eg:
  CURSOR c_unit_rate(p_cr_type transactions_mst.cr_Type%TYPE) IS
  SELECT UNIT_RATE
  FROM   TRANSACTIONS_MST T
  WHERE  T.EFF_DT    = :BLOCK_PARAM.EFF_DT
  AND    T.GRADE_CD  = :TRANSACTIONS_MST.GRADE_CD 
  AND     T.CR_TYPE   = p_cr_type;
Re: Reduce the Code [message #625158 is a reply to message #625157] Thu, 02 October 2014 02:56 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
Or you could stick the select in a function with a parameter of cr_type.
Re: Reduce the Code [message #625162 is a reply to message #625158] Thu, 02 October 2014 04:05 Go to previous messageGo to next message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
Actually I am using below procedure:
PROCEDURE PROC_DISPLAY_GRADE_CODE (COL_NAME VARCHAR2, ORDER_TYPE VARCHAR2, P_CALL NUMBER) IS
  CURSOR C1  IS
	  SELECT S.GRADE_CD,S.GRADE_DESC
	  FROM   STEEL_GRADE_MST S
	  WHERE  S.STATUS_FLAG='A'
	  AND    S.GRADE_CD NOT IN( 
				     SELECT DISTINCT T.GRADE_CD
				      FROM   TRANSACTIONS_MST T
				      WHERE  T.AUTH_BY IS NOT NULL 
				      AND    T.AUTH_DT IS NOT NULL
				      AND    T.EFF_DT = :BLOCK_PARAM.EFF_DT
				   )
    ORDER BY S.GRADE_CD;
																		DECODE(COL_NAME, 'GRADE_DESC', S.GRADE_DESC, null), null) DESC;
   
BEGIN
	GO_BLOCK('TRANSACTIONS_MST');
	FIRST_RECORD;
	FOR C IN C1 
	  LOOP
	   :TRANSACTIONS_MST.GRADE_CD   := C.GRADE_CD;
  	   :TRANSACTIONS_MST.GRADE_DESC := C.GRADE_DESC;  	   
	     
	     BEGIN
		      SELECT T.UNIT_RATE 
		      INTO   :TRANSACTIONS_MST.CAST_PRIZE
  	  	      FROM   TRANSACTIONS_MST T
  	  	      WHERE  T.EFF_DT    = :BLOCK_PARAM.EFF_DT
  	  	      AND    T.GRADE_CD  = :TRANSACTIONS_MST.GRADE_CD 
  	  	      AND    T.CR_TYPE   = 'C';
	  	 EXCEPTION
		      WHEN NO_DATA_FOUND THEN
			:TRANSACTIONS_MST.CAST_PRIZE := NULL;
	  	 END;
	
	     BEGIN
		  	SELECT T.UNIT_RATE 
		  	INTO   :TRANSACTIONS_MST.ROLL_PRIZE
		  	FROM   TRANSACTIONS_MST T
  	  		WHERE  T.EFF_DT    = :BLOCK_PARAM.EFF_DT
  	  		AND    T.GRADE_CD  = :TRANSACTIONS_MST.GRADE_CD 
  	  		AND	   T.CR_TYPE   = 'R';  	  
	  	 EXCEPTION
		  	WHEN NO_DATA_FOUND 
		  	THEN :TRANSACTIONS_MST.ROLL_PRIZE := NULL;
	  	 END;	  	 
	  
	     NEXT_RECORD;
	  END LOOP;
	
	FIRST_RECORD;
END;


Re: Reduce the Code [message #625163 is a reply to message #625162] Thu, 02 October 2014 04:13 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
In that case change c1 to outer-join to TRANSACTIONS_MST twice, once for each cr_type and get all the data in go.
Re: Reduce the Code [message #625171 is a reply to message #625163] Thu, 02 October 2014 04:48 Go to previous messageGo to next message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
I am writing the below function for that but function gives error:
CREATE OR REPLACE FUNCTION UNIT_RATE (V_EFF_DT IN DATE, V_GRADE_CD IN VARCHAR2(3), V_CR_TYPE IN VARCHAR2(2)) 
          RETURN number IS RATE NUMBER;
          BEGIN
            SELECT T.UNIT_RATE 
            INTO   RATE
	    FROM   TRANSACTIONS_MST T
  	    WHERE  T.EFF_DT    = V_EFF_DT
  	    AND    T.GRADE_CD  = V_GRADE_CD
  	    AND	   T.CR_TYPE   = V_CR_TYPE;
            RETURN RATE;
          EXCEPTION
		  		  WHEN NO_DATA_FOUND THEN
             DBMS_OUTPUT.PUT_LINE('NO DATA FOUND');
          END UNIT_RATE;
  
Re: Reduce the Code [message #625175 is a reply to message #625171] Thu, 02 October 2014 05:04 Go to previous messageGo to next message
Littlefoot
Messages: 21807
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Only if we knew WHICH error it raises ...
Re: Reduce the Code [message #625189 is a reply to message #625175] Thu, 02 October 2014 08:38 Go to previous messageGo to next message
CraigB
Messages: 386
Registered: August 2014
Location: Utah, USA
Senior Member
I agree with Littlefoot - we need to know the error number/message. One thing I noticed in your code is that your Exception Handler should have a RETURN clause, since a Function should always return something, even if there is an error. Try adding "RETURN NULL;" to your exception handler...but please tell what error you are getting!

Craig...
Re: Reduce the Code [message #625325 is a reply to message #625189] Mon, 06 October 2014 01:50 Go to previous messageGo to next message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
SQL> SHOW ERRORS;
Errors for FUNCTION UNIT_RATE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/62 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , @ % default character
The symbol ":=" was substituted for "(" to continue.

3/61 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= . ) , @ % default character
The symbol ":=" was substituted for "(" to continue.
Re: Reduce the Code [message #625326 is a reply to message #625325] Mon, 06 October 2014 02:08 Go to previous messageGo to next message
Michel Cadot
Messages: 68634
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

You cannot constrain the type in parameter list.
So "V_GRADE_CD IN VARCHAR2(3)" should be " V_GRADE_CD IN VARCHAR2"

Re: Reduce the Code [message #625328 is a reply to message #625326] Mon, 06 October 2014 02:29 Go to previous messageGo to next message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
THANK YOU...
Re: Reduce the Code [message #625331 is a reply to message #625328] Mon, 06 October 2014 03:03 Go to previous messageGo to next message
cookiemonster
Messages: 13919
Registered: September 2008
Location: Rainy Manchester
Senior Member
Or better yet "v_grade_cd IN TRANSACTIONS_MST.GRADE_CD%TYPE," Same for the other parameters and the return should be:
"RETURN transactions_mst.rate%type"
And you should pay attention to what Craig said or you'll get errors the first time the function fails to find data.
Re: Reduce the Code [message #625346 is a reply to message #625331] Mon, 06 October 2014 06:28 Go to previous message
Mayur Shinde
Messages: 60
Registered: August 2014
Location: Pune, Maharashtra
Member
Yes. I noticed that, and I correct that mistake too.
Thank you.

[Updated on: Mon, 06 October 2014 06:29]

Report message to a moderator

Previous Topic: How to change Push button color on Mouse over in forms 10g
Next Topic: menu security
Goto Forum:
  


Current Time: Tue Apr 16 05:26:57 CDT 2024