Reason for geting ORA-01422: exact fetch returns more than requested number of rows

折月煮酒 提交于 2019-12-11 06:09:18

问题


So i am working on an installer where the installer connects to a database and creates tables and populates them. Every aspect of this works correctly except when i attempt to add rows to a certian table.

declare
  retVal INTEGER;
  rptID INTEGER;
  catID INTEGER;
  wsID INTEGER;
  paramID INTEGER;
  dtID INTEGER;
begin

  select PK into catID from RPT_CATEGORY where KEYVALUE = 'ProductivityReportsCategory';
  select PK into rptID from RPT_REPORT where KEYVALUE = 'ProductivitySummaryReport2';
  select PK into wsID from RPT_WEBSVC where KEYVALUE = 'NotApplicable' and category_fk = catID;

The select statements that populate the database look like this:

  select PK into wsID from RPT_WEBSVC where KEYVALUE = 'GetMachineNameList' and category_fk = catID;
  paramID := RPT_CONFIGURATION.ADD_PARAMETER( rptID, wsID, dtID, 'Machine', 'parameters.GetProductivityDataSet3.inserterid', 4, NULL, NULL, NULL, 0, 0, 0, 'Y', 'Y', 'N', 'N', 'Y' );

There are 13 more select statements structured like this (i won't add them since they are all similar and the only difference is the stored values that would go into the table.)

My problem is that when i run the installer, i get this error in the logs upon completion:

ORA-01422: exact fetch returns more than requested number of rows 
ORA-06512: at line 30

What i would like to know is what exactly is the reason for this error to occur, and what would be the means to fix this error?

I've done some research on the topic, and found this to be the common theme of my search:

1.There's a bug in the code and the developer did not realise that you could get more than one row returned;

2.The data has been hacked rather than using the API so that validation has been broken;

3.The software is OK, what the user did was OK, but two parallel updates occurred at the same time and neither could see the uncommitted change that the other did - hence not validated correctly.

I'm positive it is not #2, but i do not quite understand what exactly the other 2 reasons mean, or how to fix them.

Any help or suggestions are greatly appreciated.

Thanks


回答1:


ORA-01422: exact fetch returns more than requested number of rows

This exception is raised whenever a SELECT INTO statement is executed and finds more than one row. A SELECT INTO statement expects to find exactly one row, no more or less - otherwise an exception is raised.

In your example:

select PK into wsID from RPT_WEBSVC
where KEYVALUE = 'GetMachineNameList'
and category_fk = catID;

it appears that there should only be one row per (KEYVALUE, CATEGORY_FK) combination, but in fact that is not the case. If there should be only one then the table should have a unique constraint on those columns:

alter table RPT_WEBSVC add constraint RPT_WEBSVC_UK
    unique (KEYVALUE, CATEGORY_FK);

That would prevent someone (or some process) adding the same row again. Of course you would need to de-duplicate the table before you could add that constraint.




回答2:


This means a "SELECT INTO" statement returned more than one row. This statement requires that only one row be returned by the query. Otherwise, you have to use a cursor loop to process the rows.

Check your select statements in SQL*Plus to see which one is the offending query.




回答3:


I would look at number 1 as a cause of the problem here.. without seeing all the statements, it's hard to tell which of these queries might return more than one row, but this is a good place to start, as database schemas can change...



来源:https://stackoverflow.com/questions/7067290/reason-for-geting-ora-01422-exact-fetch-returns-more-than-requested-number-of-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!