How to reset identity count in sql tables?

Last post 09-25-2009 3:13 AM by sabrina gage. 13 replies.

Sort Posts:

  • How to reset identity count in sql tables?

    09-09-2009, 9:55 PM
    • Member
      246 point Member
    • headmax
    • Member since 12-31-2007, 6:05 PM
    • Calgary Alberta Canada
    • Posts 230

    I want to clear all the test data out of a sql 2005 table, and reset the key to start again at 1.

    Now I know about the Truncate command, but is there a way to do this using VWD?

  • Re: How to reset identity count in sql tables?

    09-09-2009, 10:06 PM
    Answer
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    Have you tried something like this?

    DBCC CHECKIDENT (YourTableName, RESEED, 0)

    http://blog.sqlauthority.com/2007/03/15/sql-server-dbcc-reseed-table-identity-value-reset-table-identity/

    "Code,Beer and Music ~ my way of being a programmer"

  • Re: How to reset identity count in sql tables?

    09-09-2009, 10:06 PM
    Answer

    This page shows how to reset an identity: http://www.mssqlcity.com/FAQ/Devel/reset_identity_column.htm

    I would think you'd be able to setup a SqlCommand to execute this.  Don't know for sure as I haven't tried it, but it seems reasonable...

  • Re: How to reset identity count in sql tables?

    09-10-2009, 1:12 AM
    • Contributor
      3,262 point Contributor
    • basheerkal
    • Member since 06-28-2009, 3:33 AM
    • Kerala, India
    • Posts 772

     Delete the eexisting identity Field. Again add New Field (int). Set its Identity yes.

     basheerkal

    Kindly mark this post as "Answer", if it helped you.
  • Re: How to reset identity count in sql tables?

    09-10-2009, 1:23 AM
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    basheerkal:
     Delete the eexisting identity Field. Again add New Field (int). Set its Identity yes.

    I don't think if this is a good idea..


    "Code,Beer and Music ~ my way of being a programmer"

  • Re: How to reset identity count in sql tables?

    09-10-2009, 1:28 AM
    • Contributor
      3,262 point Contributor
    • basheerkal
    • Member since 06-28-2009, 3:33 AM
    • Kerala, India
    • Posts 772

    Dear Vinz,

    With due respect to your comment ..Will you pleas explain why it is not to be done?

    basheerkal

    Kindly mark this post as "Answer", if it helped you.
  • Re: How to reset identity count in sql tables?

    09-10-2009, 1:29 AM
    • Star
      7,932 point Star
    • amit.jain
    • Member since 10-06-2008, 5:09 AM
    • Delhi India
    • Posts 1,264

    DBCC CHECKIDENT (’TABLENAME’, RESEED, NEWSEEDNUMBER)

    Don't say thanks rather mark my reply as "Answer" if it helps you ,Doing so u'll get points too
    amiT jaiN

    ASP.NET C#.NET Articles
  • Re: How to reset identity count in sql tables?

    09-10-2009, 1:49 AM
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    basheerkal:
    Will you pleas explain why it is not to be done?

    IMHO, Deleting columns from your Table just because to reset the identity number is quite risky especially if that column has relationships from one table to another.


    "Code,Beer and Music ~ my way of being a programmer"

  • Re: How to reset identity count in sql tables?

    09-10-2009, 2:21 AM
    • Contributor
      3,262 point Contributor
    • basheerkal
    • Member since 06-28-2009, 3:33 AM
    • Kerala, India
    • Posts 772

     

    vinz:

    IMHO, Deleting columns from your Table just because to reset the identity number is quite risky especially if that column has relationships from one table to another.

    OK,

    Thanks a lot for explain i ng

    regards

    basheerkal

    Kindly mark this post as "Answer", if it helped you.
  • Re: How to reset identity count in sql tables?

    09-10-2009, 8:18 AM
    • Member
      246 point Member
    • headmax
    • Member since 12-31-2007, 6:05 PM
    • Calgary Alberta Canada
    • Posts 230

    vinz:

    Have you tried something like this?

    DBCC CHECKIDENT (YourTableName, RESEED, 0)

     

    Where do I type a statement like that?

  • Re: How to reset identity count in sql tables?

    09-10-2009, 8:31 AM
    • Contributor
      5,535 point Contributor
    • vijayst
    • Member since 12-19-2008, 10:49 AM
    • Posts 1,125

     In SQL Query Analyzer. But if you want to do it programatically, then use ExecuteNonQuery statement in SqlCommand.

    -Vijay
    My blog: www.VijayT.com
  • Re: How to reset identity count in sql tables?

    09-10-2009, 5:43 PM
    • Member
      246 point Member
    • headmax
    • Member since 12-31-2007, 6:05 PM
    • Calgary Alberta Canada
    • Posts 230

    vijayst:

     In SQL Query Analyzer. But if you want to do it programatically, then use ExecuteNonQuery statement in SqlCommand.

     

    Is SQL Query Analyzer part of VWD? My question was if the key reset is possible to do inside of VWD...

  • Re: How to reset identity count in sql tables?

    09-10-2009, 9:31 PM
    Answer
    • All-Star
      91,728 point All-Star
    • vinz
    • Member since 10-05-2007, 11:47 AM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    headmax:
    Is SQL Query Analyzer part of VWD? My question was if the key reset is possible to do inside of VWD...

    Query Analyzer is part of SQL Server but still you can use QA within Visual Studio by opening the Server Explorer. To open it then you can follow the following steps:

    1. In Visual Studio Go to View Tab
    2. Select Server Explorer
    3. Under Data Connections Open your DataBase
    4. Expand the Tables within your DB
    5. Find the Table that you want to use
    6. Right Click on the Table and Select "New Query"
    7. Write the Script there

    Another way would be using SqlClient objects and code it your self. Here's an example:

        private string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(GetConnectionString());
            string sqlStatement = "DBCC CHECKIDENT ('TableName', RESEED, 0)";
            connection.Open();
            SqlCommand cmd = new SqlCommand(sqlStatement, connection);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            connection.Close();
        }


    "Code,Beer and Music ~ my way of being a programmer"

  • Re: How to reset identity count in sql tables?

    09-25-2009, 3:13 AM
    • Member
      26 point Member
    • sabrina gage
    • Member since 09-22-2009, 4:24 AM
    • Posts 13

     Try VS 2008, use database project, it's very easy to manipulate the databases and tables.

    _______________________________________________________

    Sabrina Gage
    Free chat software for you

     

Page 1 of 1 (14 items)