0
votes

I have a cursor for inserting employee data from temp table to main table. I have a scenario in which first it will check if temp table department id is exist in main department table; if DeptId does not exist then don't insert employee and update ERR_MSG column in temp table. I am handling exception inside if condition after INSERT statement, but it is giving me an error like

Encountered symbol EXCEPTION when expecting one of the following : begin,end,case,else......

sample code is as below:-

DECLARE
 --var list

    BEGIN
     OPEN ltempCur For

         -- here i am selecting cols from temp table in variables

     LOOP
        FETCH ltempCur 
     INTO 
       --all the declared variables here
     EXIT
    WHEN ltempCur %NOTFOUND;
    BEGIN

     --select statement for checking if employee record exist in table

     IF(lExist == 0)  --if emloyee record not exist
     THEN

       begin
         --check if dept exist in dept table if not exist then i have assigned zero to deptid variable in NO_DATA_FOUND exception
       end;

       IF(vardeptid > 0)  --if condition for which i have mentioned in description

       --Insert statement for employee

       EXCEPTION
       WHEN OTHERS
       THEN
           --handling exp
       COMMIT;
    END;         

       END IF; -- dept id check if end

     --end loop -- close cursor

In above code, if I remove deptid IF ELSE condition then it is working fine, but with that IF condition it is giving error

1
You shouldn't put an EXCEPTION section within IF.. END IF .Use RAISE statement if you want to raise an user defined exception when a condition is satisfied. EXCEPTION section should be at the end of a BEGIN..END and can handle or re-raise required exceptions. - Kaushik Nayak
i want to check if employee insert happens successfully or not, that is why i wrote EXCEPTION after insert statement of employee, can i check rowcount after insert and raise exception if it is zero after END IF? - Learner1
If you want a specific EXCEPTION handler for inserting Employee record then you need to frame that insert statement in a proper block: BEGIN insert ... EXECPTION ... END; - APC

1 Answers

0
votes

There is a single end if against two IF clauses. Probably, you have not ended the IF clause for variable lExists.