15

How can I reset the Identity column of a table to zero in SQL Server?

Edit:

How can we do it with LINQ to SQL ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232

6 Answers6

26
DBCC CHECKIDENT (MyTable, RESEED, NewValue)

You can also do a Truncate Table, but, of course, that will remove all rows from the table as well.

To do this via L2S:

db.ExecuteCommand("DBCC CHECKIDENT('MyTable', RESEED, NewValue);");

Or, you can call a stored procedure, from L2S, to do it

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • How can we do it with LINQ to SQL ? – Mohammad Dayyan Dec 19 '10 at 19:41
  • Thanks dude, But I have a problem in executing the above command. I've choose a name for my table in UTF-8 format(Persian) and then I insert it instead of `MyTable` the following error is occurred `Cannot find a table or object with the name "[????? ?????? ???]". Check the system catalog.` How can I resolve it? – Mohammad Dayyan Dec 20 '10 at 18:33
9
DBCC CHECKIDENT ( ‘databasename.dbo.yourtable’,RESEED, 0)

More info : http://msdn.microsoft.com/en-us/library/ms176057.aspx

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrew
  • 26,629
  • 5
  • 63
  • 86
6

Use the LINQ to SQL ExecuteCommand to run the required SQL.

db.ExecuteCommand("DBCC CHECKIDENT('table', RESEED, 0);");

LINQ is a data-source agnostic query language and has no built-in facilities for this kind of data-source specific functionality. LINQ to SQL doesn't provide a specific function to do this either AFAIK.

Blake Taylor
  • 9,217
  • 5
  • 38
  • 41
3

Generic extension method:

    /// <summary>
    /// Reseeds a table's identity auto increment to a specified value
    /// </summary>
    /// <typeparam name="TEntity">The row type</typeparam>
    /// <typeparam name="TIdentity">The type of the identity column</typeparam>
    /// <param name="table">The table to reseed</param>
    /// <param name="seed">The new seed value</param>
    public static void ReseedIdentity<TEntity, TIdentity>(this Table<TEntity> table, TIdentity seed)
        where TEntity : class
    {
        var rowType = table.GetType().GetGenericArguments()[0];

        var sqlCommand = string.Format(
            "dbcc checkident ('{0}', reseed, {1})",
            table.Context.Mapping.GetTable(rowType).TableName, 
            seed);

        table.Context.ExecuteCommand(sqlCommand);
    }

Usage: myContext.myTable.ReseedIdentity(0);

diachedelic
  • 2,195
  • 1
  • 24
  • 28
3

use this code

DBCC CHECKIDENT(‘tableName’, RESEED, 0)
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138
  • This answer doesn't add anything that wasn't already contained in the previous answers, please consider deleting. – Benjol Dec 20 '10 at 12:50
0

To accomplish the same task in a SQL Compact table use:

db.CommandText = "ALTER TABLE MyTable ALTER COLUMN Id IDENTITY (1,1)";   
db.ExecuteNonQuery(  );
Florian Greinacher
  • 14,478
  • 1
  • 35
  • 53