Friday, July 27, 2012

Dealing with ORA-01427 Errors Returned by a Stored Procedure

Recently, I received this error in a email from a Stored Procedure:

ORA-01427: single-row subquery returns more than one row

Usually, I would have liked to receive a line number or some other clue as to where in this 2,000 lines of code that the error occurred (I did not write this procedure) but this is all I have to work with.

Thinking back, ever have a SQL statement that you develop to get data only to find when you go to create a Table with it (via CTAS or the like), only then are you confronted with an error? Well, this was the path I chose to solve this ORA-01427 error.

First, I noticed that this Stored Procedure has a large SQL statement broken into two parts that used a UNION statement. To begin, I created a simple CTAS statement using the first part of this SQL. It created the table without reporting any errors. This tells me that the problem was within the UNION statement. When I did the same step with the second part of the SQL, TOAD returned the error and reported the line(s) that were causing the error and even highlighted the line in my Editor.

With this information, I was able to put the fix in place (you will either need to modify your SQL or you have bad data) to get the Stored Procedure running again.

As any system "matures" with new data, sometimes data scenarios present themselves that were not originally anticipated by the Developer. Just as likely, there are times when you just have bad data and someone in the Business Group needs to correct.