BBS.PROGENIC.COM
Home      Members   Calendar   Who's On   SOTM
Welcome Guest ( Login | Register )
      

Home » Chat » Coding » @@Identity in MySql


@@Identity in MySqlExpand / Collapse
Author
Message
Posted Wednesday, September 05, 2007 7:55 AM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Monday, July 26, 2010 2:34 AM
Posts: 2,759, Visits: 38,407
I'm trying to get the row id for the record i just entered, i could select the max id for the primary key but this is going to be a huge multithreaded system and can't risk returning the wrong ID.

In mssql you have @@Identiy which can return the id of the row you added, anybody know if there is an equivalant in mysql?

 

Post #11667
Posted Wednesday, September 05, 2007 8:27 AM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Monday, July 26, 2010 2:34 AM
Posts: 2,759, Visits: 38,407
sorted it's SELECT LAST_INSERT_ID() and it can be used in c# like so:

 public int InsertContactList(objContactList list)
        {

            MySqlCommand cmd;

            conn.Open();
            cmd = new MySqlCommand("insert into contact_lists (ListName,UserID) values (?ListName,?UserID);Select LAST_INSERT_ID();", conn);
           
            cmd.Parameters.AddWithValue("?ListName", list.ListName);
            cmd.Parameters.AddWithValue("?UserID", list.UserID);

            int i = (int)(long)cmd.ExecuteScalar();
            conn.Close();
            return i;

        }



 
Post #11668
Posted Wednesday, September 05, 2007 10:12 AM


Progenic Family

Progenic Family

Group: Forum Members
Last Login: Monday, February 22, 2010 10:41 PM
Posts: 537, Visits: 1,389
just out of curiosity
where are u using this

just if ya want to say
Post #11669
Posted Wednesday, September 05, 2007 10:52 AM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Monday, July 26, 2010 2:34 AM
Posts: 2,759, Visits: 38,407
It's for an antispam system i'm working on. It's been quite interesting learning how mysql differs from mssql.

Two other differences that stumped me for a while were that mysql doesn't have GetDate() as an sql function so i had to swap that to Now(), and the one I got stuck on the longest was when you use parameterized queries, mssql variables are named like @Variable but in mysql it's ?Variable. It gave me no error messages when i used @Variable it just didn't work, took me f**king forever to work that out .

 

Post #11670
Posted Wednesday, September 05, 2007 11:28 AM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Friday, June 25, 2010 11:11 AM
Posts: 1,063, Visits: 5,883
I hope it's not actually the same as @@identity, but rather scope_identity()..

scope_identity() is the last inserted id for your scope (query, stored proc, trigger)

@@identity is last inserted id for the database for your connection

Or in other words - on busy systems with heavy inserting going on, @@identity can return the wrong id

Post #11671
Posted Wednesday, September 05, 2007 11:29 AM


Progenic Family

Progenic Family

Group: Forum Members
Last Login: Monday, February 22, 2010 10:41 PM
Posts: 537, Visits: 1,389
mhm....all i can agree with is that databasez are bitches
hed trouble with swl at absolutel every site i worked on
absolutely no exceptions
Post #11672
Posted Wednesday, September 05, 2007 12:19 PM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Monday, July 26, 2010 2:34 AM
Posts: 2,759, Visits: 38,407
wax (9/5/2007)
I hope it's not actually the same as @@identity, but rather scope_identity()..

scope_identity() is the last inserted id for your scope (query, stored proc, trigger)

@@identity is last inserted id for the database for your connection

Or in other words - on busy systems with heavy inserting going on, @@identity can return the wrong id

hmm from what I read it is for the current connection which i assume means between con.open and con.close.

Sound right to you?

 

Post #11674
Posted Wednesday, September 05, 2007 1:02 PM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Friday, June 25, 2010 11:11 AM
Posts: 1,063, Visits: 5,883
Sounds about right, not sure how ado.net's connection pooling comes into play here though, it keeps connections alive so theoretically that could mess things up. But you'd have to read up / test that really.

That being said though, any sort of trigger will definitely live inside your connection's scope, so if one of those does an insert somewhere on an identity field because of something you did, you're in trouble. Moral of the story - use scope_identity(), there's no reason not to

Post #11675
Posted Sunday, January 24, 2010 11:00 PM
New Member

New Member

Group: Forum Members
Last Login: Sunday, February 28, 2010 3:29 PM
Posts: 4, Visits: 17
he obviously is using it for mysql injection teqniques the identify string is the hard part of the injection

'1=1'OR"1"


mentor's words:"freedom of speach is yelling theater in a crowded fire"
Post #18882
Posted Monday, January 25, 2010 4:22 AM


Progenic Crew

Progenic Crew

Group: Administrators
Last Login: Monday, July 26, 2010 2:34 AM
Posts: 2,759, Visits: 38,407
orly?

 
Post #18884
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: proge, wax, stadz, Anarchy Angel, lun0s

All times are GMT -6:00, Time now is 4:56pm

Progenic BBS 2010
Execution: 0.203. 10 queries. Compression Enabled.