0

I am trying to create an insert statement based off of a loop but I am getting PLS-00103 Encountered the symbol "end of file" when expecting one of the following: ;

The point of the script is to select all IDs from another table based on the value of another field in a joined table and then insert those IDs into a second table, one row at a time of course.

Here is what I have so far and I'm not sure why I get the error. The line number points to the last CURRENT_DATE entry.

BEGIN
 For v_apv_cmp_id IN (
                            select distinct
                              paa.APV_CMP_ID
                            from APV_APVR paa
                            join APV_CMP pac on pac.apv_cmp_id = paa.apv_cmp_id
                            join SEC_DISPLAY ssd on ssd.app_level_id = pac.app_level_id
                            where ssd.field_table_name = 'application_name'
                        )
 LOOP
 INSERT INTO APV_APVR
        (APV_APVR_ID, APV_CMP_ID, APVR_USR_ID, ORG_USR_ACV_TS, ORG_USR_NU, LTS_UPD_USR_NU, LTS_UPD_USR_TS)
 values (
        (
          select MAX(APV_APVR_ID)+1 from APV_APVR
        )
          , v_apv_cmp_id
          , 'tssao18'
          , 'tssetac'
          , CURRENT_DATE
          , 'tssetac'
          , CURRENT_DATE
        );
 END LOOP;
END;
5
  • Can you just remove the value ( select MAX(PRE_APV_APVR_ID)+1 from PRE_APV_APVR ) and replace it with some dummy constant and see if it works Commented May 20, 2013 at 17:06
  • 2
    @can you use select directly instead of cursor? Commented May 20, 2013 at 17:07
  • Well, I need to increment the PK for that table. It works when inserting just one row. I was hoping to be able to use it for multimple rows. Commented May 20, 2013 at 17:08
  • Convert it into a single SQL statement and generate the id using rownum+coalesce((select max(APV_APVR_ID) from APV_APVR),0). It'll work as long as this is the only session inserting into the table at any one time Commented May 20, 2013 at 18:25
  • The usage of v_apv_cmp_id as ID is a little bit suspicious. Try to replace it with v_apv_cmp_id.apv_cmp_id. Commented May 20, 2013 at 21:03

2 Answers 2

1
DECLARE
    new_id number;
BEGIN
    SELECT NVL(MAX(APV_APVR_ID),0) + 1 into new_id FROM APV_APVR;
    FOR v_apv_cmp_id IN (
                                SELECT DISTINCT
                                  paa.APV_CMP_ID
                                FROM APV_APVR paa
                                JOIN APV_CMP pac ON pac.apv_cmp_id = paa.apv_cmp_id
                                JOIN SEC_DISPLAY ssd ON ssd.app_level_id = pac.app_level_id
                                WHERE ssd.field_table_name = 'application_name'
                            )
     LOOP
         INSERT INTO APV_APVR
            (APV_APVR_ID, APV_CMP_ID, APVR_USR_ID, ORG_USR_ACV_TS, ORG_USR_NU, LTS_UPD_USR_NU, LTS_UPD_USR_TS)
     VALUES (  new_id
              , v_apv_cmp_id
              , 'tssao18'
              , 'tssetac'
              , CURRENT_DATE
              , 'tssetac'
              , CURRENT_DATE
            );
         new_id := new_id + 1;
     END LOOP;
END;
Sign up to request clarification or add additional context in comments.

2 Comments

I like this approach of taking the select out of the values section but I actually get two 'end of file' errors with this one. Maybe I can flesh them out. The new error is on line 2: Encountered the symbol "end-of-file" when expecting one of the following: := . ( @ % ; not null range default character
If you add the tables DDL that are involved in the process, that would be helpfull
0
BEGIN
 For v_apv_cmp_id IN (
                            select distinct
                              paa.APV_CMP_ID
                            from APV_APVR paa
                            join APV_CMP pac on pac.apv_cmp_id = paa.apv_cmp_id
                            join SEC_DISPLAY ssd on ssd.app_level_id = pac.app_level_id
                            where ssd.field_table_name = 'application_name'
                        )
 LOOP
 INSERT INTO APV_APVR
        (APV_APVR_ID, APV_CMP_ID, APVR_USR_ID, ORG_USR_ACV_TS, ORG_USR_NU, LTS_UPD_USR_NU, LTS_UPD_USR_TS)
 values (
        (
          select MAX(APV_APVR_ID)+1 from APV_APVR;
        )
          , v_apv_cmp_id
          , 'tssao18'
          , 'tssetac'
          , CURRENT_DATE
          , 'tssetac'
          , CURRENT_DATE
        );
 END LOOP;
END;

1 Comment

I tried adding the semi colon to the end of the select in the values section but no luck. :-(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.