Search code examples
sqloracle-databaseora-01422

ORA-01422, but Query works fine


I'm getting absolutely crazy over this. I'm currently writing a trigger to change some dates around. For that I want to get a max value into a variable like so:

SELECT date INTO datevar
               FROM table
               WHERE date = (SELECT MAX(date)
                                    FROM table
                                    WHERE condition = loop.condition);

The trigger always throws an ORA-01422 (Too many rows returned) on the line of this query; but when I try the query in my sqldeveloper (substituting the loop value with a number) the query works fine, with any data set (works fine = returns exactly one row).

I tried different ways to select the max value into my var, but it's the same problem every time. The loop works fine (when I run the trigger as stored procedure with verbose logging, I see the loop row id's and everything), but it always starts with said error without writing to the database.

I also scoured the web, but I didn't find anything. I'd appreciate any ideas.


Solution

  • Why would you run such a complicated query? Replace it with:

    SELECT MAX(date) INTO datevar
    FROM table
    WHERE condition = loop.condition;
    

    This cannot generate such an error.