Home » SQL & PL/SQL » SQL & PL/SQL » PLSQL (oracle 8i)
PLSQL [message #599157] Tue, 22 October 2013 11:21 Go to next message
Hometown1
Messages: 23
Registered: March 2011
Junior Member
With reference to the below mentioned sql plus session, I couldn't figure out the location of the error. Can anyone shed some light on this, as to what could be the cause for this error.

I have tried prefixing the table name with schema which I am connected to named "IHELP" but the same error show up


SQL> show user
USER is "IHELP"
SQL> select banner from v$version
  2  /

BANNER
----------------------------------------------------------------
Oracle8 Enterprise Edition Release 8.0.5.0.0 - Production
PL/SQL Release 8.0.5.0.0 - Production
CORE Version 4.0.5.0.0 - Production
TNS for 32-bit Windows: Version 8.0.5.0.0 - Production
NLSRTL Version 3.3.2.0.0 - Production

SQL> desc tbl_category_detail
 Name                            Null?    Type
 ------------------------------- -------- ----
 DONATION_CODE                            NUMBER(5)
 CATEGORY_CODE                            NUMBER(5)
 CATEGORY_AMOUNT                          NUMBER(12,2)

SQL> desc tbl_donation
 Name                            Null?    Type
 ------------------------------- -------- ----
 DONATION_CODE                   NOT NULL NUMBER(8)
 DONATION_DATE                            DATE
 DONATION_RECEIPT_NO                      NUMBER(6)
 CONTACT_CODE                             NUMBER(5)
 PURPOSE_CODE                             NUMBER(5)
 COUNTRY_CODE                             NUMBER(5)
 DONATION_AMOUNT                          NUMBER(10,2)
 CATEGORY_CODE                            NUMBER(5)
 DONATION_PAYMENT_MODE                    CHAR(2)
 APPEAL_CODE                              NUMBER(5)
 DONATION_SR_NO                           NUMBER(5)
 QURBANI_TYPE                             CHAR(1)
 CURRENCY_CODE                            NUMBER(5)
 CARD_NO                                  VARCHAR2(25)
 ISSUE_DATE                               DATE
 EXPIRY_DATE                              DATE
 ISSUE_NO                                 NUMBER(2)
 CARD_TYPE                                CHAR(2)
 DONATION_BOOK_NO                         NUMBER(3)
 DONATION_MACHINE_NO                      NUMBER(3)
 DONATION_TYPE                            CHAR(2)
 DONATION_DESCRIPTION                     VARCHAR2(200)
 DONATION_CLAIM                           CHAR(1)
 CAMPAIGN_CODE                            NUMBER(5)
 CREATION_DATE                            DATE
 PROGRAM_CODE                             NUMBER(5)
 CANVASSER_NAME                           VARCHAR2(45)
 STUDENT_CODE                             NUMBER(5)
 LAST_UPDATE_DATE                         DATE
 UPDATED_FOR                              VARCHAR2(50)
 LAST_UPDATED_BY                          VARCHAR2(45)
 PLEDGE_CODE                              NUMBER(3)
 GA_CLAIM_BATCH                           VARCHAR2(20)
 CANVASSER_CODE                           NUMBER(5)
 INCOME_SOURCE                            CHAR(2)

DECLARE 
    CURSOR c1 IS 
      SELECT donation_code, 
             SUM(category_amount) 
      FROM   tbl_category_detail 
      GROUP  BY donation_code; 
BEGIN 
    FOR rec IN c1 LOOP 
        UPDATE tbl_donation 
        SET    donation_amount = rec.category_amount 
        WHERE  donation_code = rec.donation_code; 
    END LOOP; 
EXCEPTION 
    WHEN OTHERS THEN 
      dbms_output.Put_line(SQLCODE 
                           ||' ' 
                           ||SQLERRM); 
END; 

 DECLARE
*
ERROR at line 1:
ORA-06550: line 12, column 28:
PLS-00302: component 'CATEGORY_AMOUNT' must be declared
ORA-06550: line 11, column 2:
PL/SQL: SQL Statement ignored
Re: PLSQL [message #599158 is a reply to message #599157] Tue, 22 October 2013 11:45 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>PLS-00302: component 'CATEGORY_AMOUNT' must be declared
CATEGORY_AMOUNT is not part of the SELECT clause; as a separate & distinct "column"
Re: PLSQL [message #599160 is a reply to message #599157] Tue, 22 October 2013 12:17 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
Hometown1 wrote on Tue, 22 October 2013 21:51
      SELECT donation_code, 
             SUM(category_amount) 
      ...
      ...   
        SET    donation_amount = rec.category_amount 
        WHERE  donation_code = rec.donation_code; 
    


Do you want donation_amount to be set as category_amount or SUM(category_amount)?

2.
Quote:
EXCEPTION 
    WHEN OTHERS THEN 
      dbms_output.Put_line(SQLCODE 
                           ||' ' 
                           ||SQLERRM); 



The WHEN OTHERS exception block will not help you in anyway.

Regards,
Lalit
icon13.gif  Re: PLSQL [message #599163 is a reply to message #599157] Tue, 22 October 2013 12:22 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Very bad code.
1/ Remove WHEN OTHERS (read the link)
2/ All the PL/SQL can be replace by a single UPDATE

Re: PLSQL [message #599174 is a reply to message #599163] Tue, 22 October 2013 13:44 Go to previous messageGo to next message
Hometown1
Messages: 23
Registered: March 2011
Junior Member
Yes, blackswan you were right just added an alias and it worked,secondly thanks Michel for the helpful link regarding the hazards of using when others in exception handling made a note of it.

I am not sure I have used COMMIT in the Loop is this a standard practice or should be avoided, if yes then what is the proper location for commit to be used. If anyone can pass me a link on the PL-SQL best practices, that will help.

Thank You

DECLARE 
    CURSOR c1 IS 
      SELECT donation_code, 
             SUM(category_amount) category_amount 
      FROM   tbl_category_detail 
      GROUP  BY donation_code; 
BEGIN 
    FOR rec IN c1 LOOP 
        UPDATE tbl_donation 
        SET    donation_amount = rec.category_amount 
        WHERE  donation_code = rec.donation_code; 

        COMMIT; 
    END LOOP; 

    dbms_output.Put_line('Rows Updated ' 
                         || SQL%rowcount); 
/* 
EXCEPTION 
WHEN OTHERS THEN 
DBMS_OUTPUT.PUT_LINE(SQLCODE||' ' ||SQLERRM); 
*/ 
END; 
icon2.gif  Re: PLSQL [message #599176 is a reply to message #599174] Tue, 22 October 2013 13:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
I am not sure I have used COMMIT in the Loop is this a standard practice or should be avoided, i


Yes it is a bad practice because:
1/ It is not ANSI
2/ It may lead to ORA-01555 error
3/ It drastically decreases the performances, not only for you but for every one connected to the database (and a bit less to the server)

The correct place of a commit is when the transaction is completed. If the transaction is only this PL/SQL block then it is at the end of it (and better outside it).

But as I said, the whole PL/SQL block can be replaced by a single UPDATE statement (and then the question of the place of the commit is irrelevant).

[Updated on: Tue, 22 October 2013 14:00]

Report message to a moderator

Re: PLSQL [message #599177 is a reply to message #599174] Tue, 22 October 2013 13:58 Go to previous message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
never do in PL/SQL that which can be done in plain SQL

UPDATE tbl_donation TD 
SET    TD.donation_amount = (SELECT SUM(category_amount) 
                             FROM   tbl_category_detail TCD 
                             WHERE  TD.donation_code = TCD.donation_code); 


never COMMIT inside LOOP
Previous Topic: With Clause Not working
Next Topic: CAN YOU??
Goto Forum:
  


Current Time: Wed Apr 24 22:53:24 CDT 2024