简体   繁体   中英

ORA-00917: missing comma with execute immediate

I'm trying to make a function that inserts a row and return the rowcount if it succeed. Here's my code.

create or replace FUNCTION "FUNCINSERTROW"
(V_PARAM IN NVarChar2
,V_PARAM2 IN NVarChar2
,V_PARAM6 IN NVarChar2
,V_PARAM4 IN NVarChar2
,V_PARAM5 IN NVarChar2
,V_PARAM6 IN NVarChar2
,V_PARAM7 IN NVarChar2
,V_PARAM8 IN NVarChar2
,V_PARAM9 IN NVarChar2
,V_PARAM10 IN NVarChar2
,V_PARAM11 IN NVarChar2
,V_PARAM12 IN Number
,V_PARAM13 IN NVarChar2
,V_PARAM14 IN NVarChar2
,V_PARAM15 IN NVarChar2
,V_PARAM16 IN NVarChar2
,V_PARAM17 IN NVarChar2
,V_PARAM18 IN NVarChar2
,V_PARAM19 IN NVarChar2
,V_PARAM20 IN NVarChar2
,V_PARAM21 IN NVarChar2
,V_PARAM22 IN NVarChar2
,V_PARAM23 IN NVarChar2
,V_PARAM24 IN NVarChar2
,V_PARAM25 IN NVarChar2
,V_PARAM26 IN NVarChar2
,V_PARAM27 IN NVarChar2
,V_PARAM28 IN NVarChar2)

RETURN NUMBER AS 
BEGIN
DECLARE
  v_requete   varchar2(5000);
  BEGIN

v_requete := 'INSERT INTO TABLE (PARAM,PARAM2,PARAM3,PARAM4,PARAM5,PARAM6,' ||
              'PARAM7,PARAM8,PARAM9,PARAM10,PARAM11,PARAM12,PARAM13,PARAM14,PARAM15,PARAM16,PARAM17,PARAM18,PARAM19,' ||
              'PARAM20,PARAM21,PARAM22,PARAM23,PARAM24,PARAM25,PARAM26,PARAM27,PARAM28)' ||
              'VALUES ('||V_PARAM||','||V_PARAM2||','||V_PARAM3||','||V_PARAM4||','||V_PARAM5||','||V_PARAM6||','||V_PARAM7||','||V_PARAM8||
              ','||V_PARAM9||','||V_PARAM10||','||V_PARAM11||','||V_PARAM12||','||V_PARAM13||','||V_PARAM14||','||V_PARAM15||','||V_PARAM16||
              ','||V_PARAM17||','||V_PARAM18||','||V_PARAM19||','||V_PARAM20||','||V_PARAM21||','||V_PARAM22||','||V_PARAM23||','||V_PARAM24||
              ','||V_PARAM25||','||V_PARAM26||','||V_PARAM27||','||V_PARAM28||')';


  execute immediate v_requete;
  RETURN SQL%ROWCOUNT;


  EXCEPTION
      WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20996,SQLERRM || ' - ANNUDATA:' || v_requete);
  END;
END FUNCINSERTROW;

And here is the error it gives me.

ORA-20996: ORA-00917: missing comma - ANNUDATA:INSERT INTO Table (PARAM,PARAM2,PARAM3,PARAM4,PARAM5,PARAM6,PARAM7,PARAM8,PARAM9,PARAM10,PARAM11,PARAM12,PARAM13,PARAM14,PARAM15,PARAM16,PARAM17,PARAM18,PARAM19,PARAM20,PARAM21,PARAM22,PARAM23,PARAM24,PARAM25,PARAM26,PARAM27,PARAM28)VALUES (t,t,t,t,t,16,PO,t,t,t,t,7740,NO,DO,t,t,TEST,2C,t,t,t,Ben,t,t,t,997,t,t)

I read a lot of posts. Most people forget right parenthesis but I don't think it's my case. What am I missing ?

Ok, I found where the problem was. I needed to put triple quotes around NVarchar2 values. Like that.

'''||V_PARAM||''','''||V_PARAM2||''','''||V_PARAM3||''','''||V_PARAM4||'''

In case you really have to do it dynamically (which I don't believe) this solution is less typo work and better because is also works in case one of your input parameters contains a single quote ' .

    v_requete   VARCHAR2(5000);
BEGIN
    v_requete := 'INSERT INTO TABLE (PARAM';
    FOR i IN 2..28 LOOP
        v_requete := v_requete ||',PARAM'||i;
    END LOOP;
    v_requete := v_requete ||') VALUES (:p1';
    FOR i IN 2..28 LOOP
        v_requete := v_requete ||',:p'||i;
    END LOOP;
    v_requete := v_requete ||')';

    EXECUTE IMMEDIATE v_requete USING
        PARAM, PARAM2, PARAM3, PARAM4, PARAM5,
        PARAM6, PARAM7, PARAM8, PARAM9 PARAM10,
        PARAM11, PARAM12, PARAM13, PARAM14 PARAM15,
        PARAM16, PARAM17, PARAM18, PARAM19 PARAM20,
        PARAM21, PARAM22, PARAM23, PARAM24 PARAM25,
        PARAM26, PARAM27, PARAM28;   
    RETURN SQL%ROWCOUNT;

EXCEPTION
    WHEN OTHERS THEN
        RAISE_APPLICATION_ERROR(-20996,SQLERRM || ' - ANNUDATA:' || v_requete);
    END;
END FUNCINSERTROW;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM