+ Reply to Thread
Results 1 to 5 of 5

Thread: Resetting Identity

  1. #1
    JaedenRuiner's Avatar
    JaedenRuiner is offline VB.NET Forum Master JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2007
    Age
    32
    Posts
    340
    Reputation
    86

    Default Resetting Identity

    How do I force the reset of the Identity field. For instance, I'm testing the application now, and occasionally there are errors, I purge the database to redo a full test (from the beginning) and even though there are no records in my table, it's starting the identity column at 45. How do I tell the database to restart the identity from my "seed" value?

    Thanks
    Jaeden "Sifo Dyas" al'Raec Ruiner
    http://www.wayoftheleaf.net/

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    10,843
    Reputation
    1443

  3. #3
    JaedenRuiner's Avatar
    JaedenRuiner is offline VB.NET Forum Master JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2007
    Age
    32
    Posts
    340
    Reputation
    86

    Default

    awesome thanks!

    <edit>

    Well, sort of. Is there anything on the VB side that would keep the truncate from fully resetting the Identity? I did set the Typed Dataset that manages this table to have that field as an "Identity" field in the DataSource Designer.
    I've tried running the truncate normally, but It has problems with Foreign Key Constraints even when the PK doesn't point to any FK records in the child table. When the table is empty it returns fine, but it hasn't reset the identity, which means either the Foreign Key is getting in the way, or VB is keeping the Identiy "alive" with the typed dataset in memory and I need to close down or something.

    Thanks
    Last edited by JaedenRuiner; 10-14-2008 at 9:45 AM.
    Jaeden "Sifo Dyas" al'Raec Ruiner
    http://www.wayoftheleaf.net/

  4. #4
    JaedenRuiner's Avatar
    JaedenRuiner is offline VB.NET Forum Master JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now JaedenRuiner probably authored a book by now
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Aug 2007
    Age
    32
    Posts
    340
    Reputation
    86

    Default

    Well,

    How do Foreign Key constraints affect Truncate? Because here's the thing, the Delete Rule is set to "Set Null", the Update is set to "Cascade" i've deleted from the parent table, and from the child table so there are no rows anywhere, but truncate still says it can't do it due to a foreign key constraint.
    What's that all about, eh?

    <edit>
    Never mind, looked into the DBCC commands and that worked beautifully.


    Thanks
    Jaeden "Sifo Dyas" al'Raec Ruiner
    http://www.wayoftheleaf.net/

  5. #5
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute JohnH has a reputation beyond repute
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Age
    37
    Posts
    10,843
    Reputation
    1443

    Default

    This is what the Transact-SQL Reference documentation says:
    TRUNCATE TABLE is functionally identical to DELETE statement with no WHERE clause
    <snip>blah blah logging and speed<snip>
    You cannot use TRUNCATE TABLE on a table referenced by a FOREIGN KEY constraint; instead, use DELETE statement without a WHERE clause.

    If you want to retain the identity counter, use DELETE instead.
    So it's clear on foreign keys, while I don't agree when it says "functionally identical". For this the DBCC should be of help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts