Quantcast

Return value from update with database triggers on updated table

classic Classic list List threaded Threaded
3 messages Options
slt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Return value from update with database triggers on updated table

slt
I have a scenario using MyBatis 3.0.5/Spring 3.1.0 and SQL Server
2008. I have a simple mapped update statement that updates a table
which also has a trigger for logging audit data.

Without the trigger, an update by primary key returns a value of 1 as
expected - showing that 1 record was affected.  However, with the
trigger enabled (which causes additional queries to execute) the
return value is unpredictable because it seems to return the rowcount
from the first statement excecuted in the trigger (which is not the
mapped update).

How is the MyBatis return value for an update statement determined?

Is there a way to specify which value is returned? (in my case this is
the last statement executed, after the trigger has done it's work).

Thanks.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value from update with database triggers on updated table

Eduardo Macarron
This way:

    PreparedStatement ps = (PreparedStatement) statement;
    ps.execute();
    int rows = ps.getUpdateCount();

On 14 mar, 19:24, ji <[hidden email]> wrote:

> I have a scenario using MyBatis 3.0.5/Spring 3.1.0 and SQL Server
> 2008. I have a simple mapped update statement that updates a table
> which also has a trigger for logging audit data.
>
> Without the trigger, an update by primary key returns a value of 1 as
> expected - showing that 1 record was affected.  However, with the
> trigger enabled (which causes additional queries to execute) the
> return value is unpredictable because it seems to return the rowcount
> from the first statement excecuted in the trigger (which is not the
> mapped update).
>
> How is the MyBatis return value for an update statement determined?
>
> Is there a way to specify which value is returned? (in my case this is
> the last statement executed, after the trigger has done it's work).
>
> Thanks.
slt
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Return value from update with database triggers on updated table

slt
Thanks - I discovered that the behaviour I discribed in my initial
post is easily corrected for my purposes by adding "set nocount on" to
the database trigger to prevent any rowcounts being returned.

On Mar 14, 2:28 pm, Eduardo <[hidden email]> wrote:

> This way:
>
>     PreparedStatement ps = (PreparedStatement) statement;
>     ps.execute();
>     int rows = ps.getUpdateCount();
>
> On 14 mar, 19:24, ji <[hidden email]> wrote:
>
>
>
> > I have a scenario using MyBatis 3.0.5/Spring 3.1.0 and SQL Server
> > 2008. I have a simple mapped update statement that updates a table
> > which also has a trigger for logging audit data.
>
> > Without the trigger, an update by primary key returns a value of 1 as
> > expected - showing that 1 record was affected.  However, with the
> > trigger enabled (which causes additional queries to execute) the
> > return value is unpredictable because it seems to return the rowcount
> > from the first statement excecuted in the trigger (which is not the
> > mapped update).
>
> > How is the MyBatis return value for an update statement determined?
>
> > Is there a way to specify which value is returned? (in my case this is
> > the last statement executed, after the trigger has done it's work).
>
> > Thanks.- Hide quoted text -
>
> - Show quoted text -
Loading...