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
)
The quickest solution may be for you to contact the database administrator. The database administrator can either grant you appropriate privileges to freely execute any operations. Another method is that the database administrator can execute the specific operation for you.
This is where the error is being thrown. When an ORA-01422 is triggered, your SELECT INTO statement is retrieving multiple rows of data or none at all. If it is returning multiple, the predefined exception TOO_MANY_ROWS will be raised, and for no returns the PL/SQL will raise NO_DATA_FOUND.
Find the trigger that is causing the problem and disable it or drop it to resolve the issue. Usually, this error occurs in the Oracle database by the system level triggers on DDL or SYSTEM events. Another example of error ORA-00604 is when the user attempts to run a newly-created table using SELECT* from cdc.
Resolving The ProblemDelete the entries inside BATCH_ID and re-run. Notice that there is a column called 'BATCH_ID'. If the rows beneath this column name are filled in (for example with '35') then it means that a BATCH_ID number has already been assigned. Test by re-running the script.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With