1

Possible Duplicate:
How to get last inserted id?

I have a table Absences

|  Id  |  Name  |  Job  |
-------------------------
|  1   |  James |   1   |
-------------------------
|  2   |  Simon |   1   |
-------------------------

Where ID is an identity Primary Key incrementing by 1.
I'm accessing this table from a program in C# and I need to do the following :

Insert Into Absences (Name, Job) Values ('aName', 'aJob')

The problem is I need to get the Id column where i'm inserting at the same time because Nameand Job are not unique so I won't be able to retreive this exact column after.

Is it possible to add a select on the Id column in that query ?


Update

  SqlConnection myConnection = new SqlConnection(@"SomeConnection");
  myConnection.Open();
  SqlCommand myCommand = myConnection.CreateCommand();
  myCommand.CommandText = "Insert Into Absences (Name, Job) Values ('aName', 'aJob')";
  int currentAbs = (int)myCommand.ExecuteScalar();

I get an error on the ExecuteScalar Line. Object reference is not set to and instance of object.

Community
  • 1
  • 1
phadaphunk
  • 12,785
  • 15
  • 73
  • 107
  • [Scope identity sometimes returns incorrect value](https://connect.microsoft.com/SQLServer/feedback/details/328811/scope-identity-sometimes-returns-incorrect-value). Use an `OUTPUT` clause as suggested in workarounds. – HABO Aug 01 '12 at 12:53
  • @HABO - Is this bug also confirmed in SQL 2008? – Phil Murray Aug 01 '12 at 13:02
  • @PhilMurray - AFAIK, it was fixed during the lifetime of 2008, but I haven't found a statement as to precisely when. It is marked as "Closed as fixed." – HABO Aug 01 '12 at 13:07
  • I changed the type cast to `Convert.ToInt32(myCommand.ExecuteScalar());` and it worked... any idea why ? – phadaphunk Aug 01 '12 at 13:08

4 Answers4

2

The SQL statement SCOPE_IDENTITY() will give you the value of the identity column of the newly inserted row from within the same scope.

SqlConnection myConnection = new SqlConnection(@"SomeConnection");
myConnection.Open();
SqlCommand myCommand = myConnection.CreateCommand();
myCommand.CommandText = "Insert Into Absences (Name, Job) Values ('aName', 'aJob'); SELECT SCOPE_IDENTITY();";
int currentAbs = (int)myCommand.ExecuteScalar();

Scope Identity Definition

Dave New
  • 38,496
  • 59
  • 215
  • 394
1

If you use SqlCommand, then you can use

int lastId = (int)command.ExecuteScalar();

to retrieve the unique id of the inserted record.
Take a look at Microsoft page.

Marco
  • 56,740
  • 14
  • 129
  • 152
0

After this query you can select @@identity to get the last inserted id in mssql server.

PepeDeLew
  • 346
  • 4
  • 15
0

One way would be to use SELECT @@IDENTITY immediately after you insert your record:

int id;
string query = "Insert Into Absences (Name, Job) Values ('aName', 'aJob')";
using (SqlCommand cmd = new SqlCommand(query, connection)) {
    connection.Open();
    // execute your INSERT query
    cmd.ExecuteNonQuery();
    // get the last-inserted ID
    cmd.CommandText = "SELECT @@IDENTITY";
    id = (int)cmd.ExecuteScalar();
}
newfurniturey
  • 37,556
  • 9
  • 94
  • 102