The Temple of Fu

code, systems and games

Truncate and DBCC

leave a comment »

So while developing some applications I have the need to clean a table and reset identity fields. Let it be known that if any of the fields are foreign keys in other tables those tables need truncated as well, working from last linked table forward to the Primary key table.

In the below example make sure to put in place of myDatabase and myTable your database that contains the table to be truncated. =]


use myDatabase
truncate table myTable


use myDatabase
DBCC CHECKIDENT ('myTable', RESEED, 0)

Advertisements

Written by lordfu

June 25, 2010 at 2:25 pm

Posted in Database, MSSQL

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: