PLS-00394: Wrong number of values in the INTO list of a fetch statement

后端 未结 3 1853
感动是毒
感动是毒 2021-01-28 11:28

This is my attempt in creating a cursor in a stored procedure

   --Second Stored Procedure--
 CREATE OR REPLACE PROCEDURE sp_GetDiscountedRate (DiscountCode IN           


        
相关标签:
3条回答
  • 2021-01-28 12:10

    This is what happens when you overcomplicate things, I'm afraid. I don't see any need for all those variables and the corresponding fetch into that is hard to keep in sync. Why not just:

    create or replace procedure sp_getdiscountedrate
        ( discountcode in varchar2
        , percentage   in number
        , reservedate  in date )
    is
    begin
        for r in (
            select p.passengerid
                 , p.first
                 , p.middle
                 , p.last
                 , p.passengertype
                 , u.usmilitaryid
                 , u.militarybranch
                 , u.militarydiscountcode
                 , r.flightnumber
                 , r.reservationcost
                 , r.reservationdate
                 , case u.militarydiscountcode
                       when discountcode then percentage * r.reservationcost
                       else r.reservationcost
                   end as revised_reservation_cost
            from   passenger p
                   join us_military u
                        on   u.mpassengerid = p.passengerid
                   join reservation r
                        on   r.passengerid = p.passengerid
                        and  r.discountcode = u.militarydiscountcode
                        and  r.reservedate = r.reservationdate
        )
        loop
            dbms_output.put_line('CUSTOMER INFORMATION:');
            dbms_output.put_line('The PassengerID is:  ' || r.passengerid);
            dbms_output.put_line('First Name of passenger is:  ' || r.first);
            dbms_output.put_line('Middle Name of passenger is:  ' || r.middle);
            dbms_output.put_line('Last Name of passenger is:  ' || r.last);
            dbms_output.put_line('Passenger Type of customer is: ' || r.passengertype);
            dbms_output.put_line('US Military ID of Passenger is:  ' || r.usmilitaryid);
            dbms_output.put_line('Military Branch of passenger is:  ' || r.militarybranch);
            dbms_output.put_line('Military Discount code of passenger is:  ' || r.militarydiscountcode);
            dbms_output.put_line('Flight number of passenger is:  ' || r.flightnumber);
            dbms_output.put_line('Reservation Cost of passenger is:  ' || r.reservationcost);
            dbms_output.put_line('Reservation Date of passenger is:  ' || r.reservationdate);
            dbms_output.put_line('Revised reservation cost is:  ' || r.revised_reservation_cost);
        end loop;
    
    end sp_getdiscountedrate;
    

    (Untested)

    0 讨论(0)
  • 2021-01-28 12:29

    You go through all the trouble of calculating "REVISED_RESERVATION_COST", and then you don't read it from the cursor.

    No wonder Oracle is complaining. You need a variable for this column as well.

    I would also advise you to learn to use modern, proper, explicit JOIN syntax.

    0 讨论(0)
  • 2021-01-28 12:30

    You have

    12 columns in your cursor's SELECT part

    but

    11 columns in FETCH statement part

    , i observe

    CASE U.MilitaryDiscountCode WHEN DiscountCode THEN   
      Percentage*R.ReservationCost 
      ELSE R.ReservationCost END "REVISED_RESERVATION_COST"
    

    part is missing in FETCH( or should be omitted in the SELECT part ).

    It also represents a column, and it spoils 1-1 correspondence, which yields that error.

    0 讨论(0)
提交回复
热议问题