views:

319

answers:

2

My procedure:

CREATE OR REPLACE PROCEDURE akcia_nepozicane_s_kurzorom
    (denny_poplatok IN NUMBER,
     kilometrovy_poplatok IN NUMBER)
AS
    my_id_auto NUMBER(5);
    my_poplatok_denny NUMBER(4);
    my_poplatok_km NUMBER(2);
    CURSOR c1 IS
        SELECT id_auto, poplatok_denny, poplatok_km FROM Auta;
BEGIN
    OPEN c1;
    LOOP
        FETCH c1 INTO my_id_auto, my_poplatok_denny, my_poplatok_km;
        EXIT WHEN c1%NOTFOUND;
        IF my_poplatok_km >= 10 THEN
            UPDATE Auta SET
                poplatok_denny = denny_poplatok,
                poplatok_km = kilometrovy_poplatok
            WHERE id_auto = my_id_auto;
        ELSE
            UPDATE Auta SET
                poplatok_denny = denny_poplatok,
            WHERE id_auto = my_id_auto;        
        END IF;
        COMMIT;
    END LOOP;
    CLOSE c1;
END;

When I try run that code in Oracle 10g Express Edition, I get this error though:

ERROR at line 23: PL/SQL: ORA-01747: invalid user.table.column, table.column, or column specification

1. CREATE OR REPLACE PROCEDURE akcia_nepozicane_s_kurzorom
2.     (denny_poplatok IN NUMBER,
3.      kilometrovy_poplatok IN NUMBER)
4. AS

I don't see any reserved words in my code so why am I getting this error?

+3  A: 

You have an extraneous comma at

        UPDATE Auta SET
            poplatok_denny = denny_poplatok,
        WHERE id_auto = my_id_auto;
Jim Garrison
Silly mistake :)
Richard Knop
Happens to ALL of us!
Jim Garrison
+4  A: 

Probably a mistake you would have avoided if you followed the Golden Rule: Never use PL/SQL when you can use SQL

CREATE OR REPLACE PROCEDURE akcia_nepozicane_s_kurzorom 
    (denny_poplatok IN NUMBER, 
     kilometrovy_poplatok IN NUMBER) 
AS 
BEGIN 

  UPDATE Auta
  Set    poplatok_denny = akcia_nepozicane_s_kurzorom.denny_poplatok,
         poplatok_km    = Case When Auta.poplatok_km >= 10
                               then akcia_nepozicane_s_kurzorom.kilometrovy_poplatok 
                               Else Auta.poplatok_km
                          End;
  COMMIT; 
END akcia_nepozicane_s_kurzorom ;

Not checked for syntax (or that it exactly matches your logic incidentally), but this is faster and less complex than the explicit cursor methodology. also the commit inside the cursor is very bad practice.

David Aldridge
+1 for "commit inside the cursor is very bad practice"; assuming you mean bad practice as in "causes the program to fail indeterministically with ORA-01555 Snapshot Too Old errors"
ammoQ
Partly because of ORA-01555's, but also because of decreased performance due to log file syncs, and that it is usually not the end of a business transaction which is where the commit ought to happen.
David Aldridge