Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

In transaction, @@IDENTITY return other table id, is it a bug?

Open youbl opened this issue 4 years ago • 10 comments

My project use: Dapper 1.50.2 Dapper.Contrib 2.0.0 DapperExtensions 1.6.3

and my code like this:

var sql = "insert into tb(name) select 'aaa' from dual where not exists(select 1 from tb where name='aaa')";
using(var connection = factory.CreateConnection())
using(var trans = connection.BeginTransaction())
{
    if(connection.Execute(sql, null, trans) > 0) 
    {
        return Convert.ToInt64(connection.ExecuteScalar("SELECT @@IDENTITY", null, trans));
    }
}
return 0;

while I published the code, I found "SELECT @@IDENTITY" returns other table's autoId, not tb's autoId? Is my code wrong? how to fix it?

youbl avatar Mar 08 '21 08:03 youbl

I tried use single sql like : insert into tb(name) select 'aaa' from dual where not exists(select 1 from tb where name='aaa'; select @@IDENTITY

but it always return id, even if insert SQL failed.

youbl avatar Mar 08 '21 08:03 youbl

Maybe you have scope_identity in mind... https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql?view=sql-server-ver15

AdaskoTheBeAsT avatar Mar 08 '21 08:03 AdaskoTheBeAsT

Maybe you have scope_identity in mind... https://docs.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql?view=sql-server-ver15

thanks, but my db is MySQL5.7.25-log, not SQL Server.

youbl avatar Mar 08 '21 10:03 youbl

My bad I see - what about last_insert_id? https://dev.mysql.com/doc/c-api/8.0/en/getting-unique-id.html

AdaskoTheBeAsT avatar Mar 08 '21 10:03 AdaskoTheBeAsT

@AdaskoTheBeAsT @@IDENTITY This variable is a synonym for the last_insert_id variable. It exists for compatibility with other database systems. See it here: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html

youbl avatar Mar 09 '21 01:03 youbl

Indeed you're right! Thanks :)

AdaskoTheBeAsT avatar Mar 09 '21 01:03 AdaskoTheBeAsT

Might be the missing closing parenthesis in your "not exists" sql clause?

fretje avatar Apr 08 '21 15:04 fretje

Might be the missing closing parenthesis in your "not exists" sql clause?

Thanks,this code is demo, my real code is right.

youbl avatar Apr 13 '21 12:04 youbl

Anyone know if this is still an issue?

davidein avatar Sep 23 '25 15:09 davidein

This is simply a limitation of the underlying database engine. Dapper issues what it is told, and processes what it gets back; what happens at the server: not up to us. However! If you're using @@identity: stop doing that - use scope_identity() or something else more appropriate. The optional Dapper.Advisor analyzer package has rules that will detect you using @@identity and issue appropriate guidance (specifically: rule DAP203). The notes here are based on SQL Server, so if you have a different database engine: use the most appropriate alternative for that engine, but once again: what happens here isn't up to Dapper.

mgravell avatar Sep 23 '25 16:09 mgravell