How To Fix ORA-06512 Errors in Oracle DB
Database mavens will recognize the name Oracle as a powerhouse in the relational database management system (DBMS) world. Oracle has produced very high-powered DBMS solutions for decades and remains a leader in the field. Many end-users of database products may be confused or baffled when they are using a predesigned solution and it generates an error message. One common error message produced by Oracle is the ORA-06512 error.
Oracle is a database management system that has been around for forty years in various forms. It originally used something called the SCOTT schema, named after one of the original employees of Oracle. You even logged into Oracle for the first time with the username ‘scott’ and the password ‘tiger’ which is named after Scott’s cat. Now there are several schemas used depending on what you use Oracle for.
If you would like to learn more about Oracle from the ground up, this page is very useful.
Fixing ORA-06512 errors
In Oracle, an ORA-06512 error is a general exception error that tells you where something is going wrong. It is one of the least specific errors produced by Oracle, as it tells you only that there is a problem, but not what is going wrong.
For example, a typical error message might read:
“ORA-01422: exact fetch returns more than requested number of rows
ORA-06512: at “DATABASE_NAME”, line 66
ORA-06512: at line 1″
The first line tells you what type of error is happening, in this case, the query is returning more data than the query expects so it doesn’t know how to handle it. The ‘ORA-01422’ code is the actual error code you need to be looking at. The ORA-06512 is just the general error code.
The second line tells you where the error is occurring. The DATABASE_NAME is going to be whatever database you are working in at the time. Line 66 is the line where the error is happening and is the line you need to check to correct the error.
The third line in the error syntax tells you where the call comes from. Check line one and you will see a call to the DATABASE_NAME.
To fix this particular error, you need to fix the issue caused by ORA-01422, which is ‘exact fetch returns more than requested number of rows’ or you need to add an exception handler to tell Oracle to ignore it. As fixing the core issue is always preferable, this is the way to go.
There are two things you can do. If you expect the query to return more than a single row, you can modify it so it doesn’t get surprised. If you only expect the query to return a single row, you can modify it for that too.
Expecting more than a single row:
for X in ( select * from t where … )
loop
— process the X record here
end loop;
This should eradicate the error in database queries where more than one row would be returned.
If you only expect a single row to be returned, you could try:
begin
select * into ….
from t where ….
process….
exception
when NO_DATA_FOUND then
error handling code when no record is found
when TOO_MANY_ROWS then
error handling code when too many records are found
end;
This second method should deliver just the single row without throwing up the ‘ORA-01422: exact fetch returns more than requested number of rows’ error and therefore the original ORA-06512 error.
You can also tweak the query so that it only returns the first row of a multiple row answer. This might work if you don’t have complete control over the database or don’t want to mess with things too much but still need an answer.
declare
c1 cursor for select * from t where …
begin
open c1;
fetch c1 into ..
if ( c1%notfound ) then
error handling for no record found
end if;
close c1;
end;
(If you know SQL, you might be a little confused by these command lines…Oracle doesn’t use Transact-SQL but rather it’s own procedural language extension of SQL, PL/SQL. While similar to Transact-SQL, PL/SQL does a lot of clever things and is a very powerful tool in its own right. You might find this PL/SQL FAQ useful when trying to learn about Oracle.)
So the basic lesson here is than an ORA-06512 error, in and of itself, isn’t something you can fix directly. Instead you have to figure out what the actual error is, which the other error codes will tell you, and then address those errors one by one.
Do you have any Oracle tips or tricks to share? Let us know about them in the comments!