Nhibernate TooManyRowsAffectedException Oracle

Viewed 103

There is a trigger on a table that periodically creates an insert and throws the TooManyRowsAffectedException. In Sql server, we can set the trigger to NoCount to solve the issue. Any ideas in Oracle?

FluentNHibernate 2.12 .net 4.7.2 Oracle 11g

1 Answers

I think you are talking about two different things:

SET NOCOUNT ON

Stops the message that shows the count of the number of rows affected by a Transact-SQL statement or stored procedure from being returned as part of the result set. When SET NOCOUNT is ON, the count is not returned. When SET NOCOUNT is OFF, the count is returned.

I guess you are talking about the exception Too_Many_Rows in Oracle, as the TooManyRowsAffectedException of Hibernate indicates that more rows were affected then we were expecting to be. Typically indicates presence of duplicate "PK" values in the given table.

The TOO_MANY_ROWS Exception (ORA-01422) occurs when a SELECT INTO statement returns more than one row.

An exception in Oracle is handled within a PL/SQL program into the exception section, which its counterpart in SQL Server would be the TRY CATCH.

When a program in Oracle contains an exception block, you can control the output of one of many specific errors by changing the outcome of them, thereby you can control what the program should do when an error happens. An exception is basically a logical expression that answers a simple question: when an error happens what do you do with it.

exception 
when ... then ...

Let me show you an example

SQL> create table t ( c1 number , c2 number ) ;

Table created.

SQL> alter table t add primary key (c1) ;

Table altered.

SQL> set timing off
SQL> declare
     begin
       insert into t values ( 1 , 1 );
       commit ;
       insert into t values ( 1 , 2 );
       commit;
    exception
      when dup_val_on_index then null;
      when others then raise;
   end;
   /

PL/SQL procedure successfully completed.

SQL> select * from t ;

        C1         C2
---------- ----------
         1          1

As you can see above in that example, I just controlled the exception dup_val_on_index to prevent the program to throw an error. I could have done the same for a too_many_rows exception.

Basically, if you want to ignore the exception, you can change the code in your trigger to avoid that when the exception happens an error is raised. You can also disable the trigger, but I guess that might be not an option.

You only need to realise that both things are different. SET NOCOUNT ON is preventing message of affected rows to be delivered to the client program. An exception in Oracle PL/SQL is used to control and handling a specific error.

Related