0

I am working on a content management system for updates where the administrator can add, edit and delete updates.

In the form where the admin adds an update, I want to create a textbox which displays the next value of an isIdentity column from the database called 'Update_ID'. I can't just retrieve the last row from the database and add 1 to it since if the admin deletes the last record in the database, the value in the textbox will not reflect the real 'Update_ID' of the next row.

How can this be solved please? Is there a way to get the actual value of the next isIdentity column in the database? Thanks :)

5
  • 4
    Wherefore do you need to diplay that value since it might be different just one millisecond later? It can only be trusted after it was created and retrieved via SCOPE_IDENTITY(SQL-Server). Mar 21, 2012 at 13:16
  • @Tim Schmelter I need to display the value on the 'Add Update' form. However, I need to use this value in an SQL statement when adding images from the same form and this is when the real problem occurs.
    – Matthew
    Mar 21, 2012 at 13:18
  • @Steve I am using SQL Server 2008. My problem is that if the admin deletes the last row with identity 23 and later adds an update, the value in the textbox will display 23 and the number 23 is used when adding images from the same form, even though the actual value of the row to be inserted is 24.
    – Matthew
    Mar 21, 2012 at 13:20
  • As a result, the new record will have an 'Update_ID' of 24 in the 'Updates' table and the new images will have an 'Update_ID' of 23 in the 'Images' table. As a result, I have lost the relationship between the images and the update. Moreover, the images may now be associated with another update has nothing to do with them.
    – Matthew
    Mar 21, 2012 at 13:22
  • 1
    Have a look my answer here which shows how to retrieve the new id. You can then use this id as foreign key for your image table. Or you could wrap both in one stored-procedure (and transaction). Mar 21, 2012 at 13:23

2 Answers 2

3

If you're interested in doing something like this, the only effective way is going to be to actually insert the blank row and return that row to the user for editing, because you can't know what the next ID is going to be without locking the table for inserts, which is probably not what you want to do.

A more effective solution might be to not fill out the Update_ID field when the user enters the data initially, but then inform them of the ID you just created afterwards by selecting SCOPE_IDENTITY or returning the value from the insert statement.

EDIT: One possible alternative for what you want to do (if you choose to upgrade to SQL Server 2012) is to use the new Sequences feature. I've never used it before, though.

3
  • Thanks :) The problem is that the same 'Update_ID' number is used when inserting images from the same form. Please see the last two comments under my question.
    – Matthew
    Mar 21, 2012 at 13:23
  • I find the comments to be very confusing. I'm not sure why you're juggling IDs -- it's hard to understand from those comments what the design of the system is supposed to be. Either way, knowing the next IDENTITY value (presumably with IDENT_CURRENT) and using that knowledge to tag associated data to an insert that hasn't occurred yet is a recipe for disaster. Mar 21, 2012 at 13:33
  • @Matthew: See edits. I wonder if you'll have access to SQL 2012? ;-) Mar 21, 2012 at 15:20
1

You can get the last inserted identity value on session or table scope, here is some reading:

But you must remember that those values are changing with every insert into table, so you can't trust them. The best way is to not show this values in front. In case of related resources you can create a stored procedure (or just an inline T-SQL block) which would make the insert into the master table, retrieve the SCOPE_IDENTITY and then make inserts to child tables with retrieved value.

EDIT

Just an example of what I have described above (with ADO.NET):

using (SqlConnection connection = new SqlConnection(_connectionString))
{
    connection.Open();

    SqlTransaction transaction = connection.BeginTransaction();

    int insertedIdentity;
    using (SqlCommand command = new SqlCommand("INSERT INTO TableNames(ColumnsList) VALUES(@ValuesList) SELECT @InsertedIdentity = SCOPE_IDENTITY()", connection, transaction))
    {
        command.CommandType = CommandType.Text;
        //Add any parameters required by INSERT here, then add parameter for SCOPE_IDENTITY
        ...
        command.Parameters.Add(new SqlParameter("@InsertedIdentity", SqlDbType.Decimal) { Direction = ParameterDirection.Output });
        command.ExecuteNonQuery();
        insertedIdentity = Convert.ToInt32(command.Parameters["@InsertedIdentity"].Value);
    }

    //Now you can make all the child inserts using insertedIdentity and transaction
    ...

    transaction.Commit();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.