Oracle Error ORA-06512
Asked Answered
C

3

13

Just can't figure out why it gives me ORA-06512 Error

PROCEDURE PX(pNum INT,pIdM INT,pCv VARCHAR2,pSup FLOAT)
AS
    vSOME_EX EXCEPTION;

BEGIN 
    IF ((pNum < 12) OR (pNum > 14)) THEN     
        RAISE vSOME_EX;
    ELSE  
        EXECUTE IMMEDIATE  'INSERT INTO M'||pNum||'GR (CV, SUP, IDM'||pNum||') VALUES('||pCv||', '||pSup||', '||pIdM||')';
    END IF;
END PX;

The structure base for the table where the insert is made:

CREATE TABLE "DB"."M12GR" (
    "IDM12GR" NUMBER(10,0) NOT NULL ENABLE, 
    "CV" VARCHAR(5) NOT NULL ENABLE, 
    "SUP" FLOAT(126) NOT NULL ENABLE, 
    "IDM12" NUMBER(10,0) NOT NULL ENABLE, 

    CONSTRAINT "PRIMARY_30" PRIMARY KEY ("IDM12GR"),
    CONSTRAINT "M12SUELORM12" FOREIGN KEY ("IDM12") REFERENCES "DB"."M12" ("IDM12") ENABLE
)
Copse answered 19/10, 2011 at 1:11 Comment(3)
What is the full error stack? ORA-06512 is just the line number (it would be helpful to include that), the actual error is in the error stack.Wrap
ORA-06512: ON "DB.PX", LINE 11 ORA-06512: ON LINE 12Copse
You can and should edit your question to include additional information instead of posting it as comments, where it is easy to miss and hard to read.Woodcutter
W
40

ORA-06512 is part of the error stack. It gives us the line number where the exception occurred, but not the cause of the exception. That is usually indicated in the rest of the stack (which you have still not posted).

In a comment you said

"still, the error comes when pNum is not between 12 and 14; when pNum is between 12 and 14 it does not fail"

Well, your code does this:

IF ((pNum < 12) OR (pNum > 14)) THEN     
    RAISE vSOME_EX;

That is, it raises an exception when pNum is not between 12 and 14. So does the rest of the error stack include this line?

ORA-06510: PL/SQL: unhandled user-defined exception

If so, all you need to do is add an exception block to handle the error. Perhaps:

PROCEDURE PX(pNum INT,pIdM INT,pCv VARCHAR2,pSup FLOAT)
AS
    vSOME_EX EXCEPTION;

BEGIN 
    IF ((pNum < 12) OR (pNum > 14)) THEN     
        RAISE vSOME_EX;
    ELSE  
        EXECUTE IMMEDIATE  'INSERT INTO M'||pNum||'GR (CV, SUP, IDM'||pNum||') VALUES('||pCv||', '||pSup||', '||pIdM||')';
    END IF;
exception
    when vsome_ex then
         raise_application_error(-20000
                                 , 'This is not a valid table:  M'||pNum||'GR');

END PX;

The documentation covers handling PL/SQL exceptions in depth.

Woodcutter answered 19/10, 2011 at 3:44 Comment(0)
P
2

The variable pCv is of type VARCHAR2 so when you concat the insert you aren't putting it inside single quotes:

 EXECUTE IMMEDIATE  'INSERT INTO M'||pNum||'GR (CV, SUP, IDM'||pNum||') VALUES('''||pCv||''', '||pSup||', '||pIdM||')';

Additionally the error ORA-06512 raise when you are trying to insert a value too large in a column. Check the definiton of the table M_pNum_GR and the parameters that you are sending. Just for clarify if you try to insert the value 100 on a NUMERIC(2) field the error will raise.

Pied answered 19/10, 2011 at 1:50 Comment(4)
Can´t do this since it goes on pairs; VALUES('''||pCv||''', ' still, the error comes when pNum is not between 12 and 14; when pNum is between 12 and 14 it does not failCopse
Could you attach the DDL of one of your M_pNum_GR tables? And are you sure that all have the same structure?Pied
CREATE TABLE "DB"."M12GR" ("IDM12GR" NUMBER(10,0) NOT NULL ENABLE, "CV" VARCHAR(5) NOT NULL ENABLE, "SUP" FLOAT(126) NOT NULL ENABLE, "IDM12" NUMBER(10,0) NOT NULL ENABLE, CONSTRAINT "PRIMARY_30" PRIMARY KEY ("IDM12GR") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT) TABLESPACE "DB" ENABLE, CONSTRAINT "M12SUELORM12" FOREIGN KEY ("IDM12") REFERENCES "DB"."M12" ("IDM12") ENABLE)Copse
Also, there are 3 tables; with the same structureCopse
F
0

I also had the same error. In my case reason was I have created a update trigger on a table and under that trigger I am again updating the same table. And when I have removed the update statement from the trigger my problem has been resolved.

Farm answered 15/11, 2019 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.