Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: SQL
Expertise: Beginner
Mar 22, 2005



Application Security Testing: An Integral Part of DevOps

Reset the Identity for All User Tables

Before you start testing any database-driven application, the first thing you need to do is:
  1. Remove the old data from all the tables.
  2. Reset the identity back to 1 for every one of the tables.
If you've got a lot of tables, this process can get quite tedious.

The following is a stored procedure which automates the whole process:

create procedure ResetIdentityForAllUserTables
   declare @tblName varchar(30);
   declare @SqlQuery varchar(3000);
   declare curResetIdentity cursor for SELECT name FROM sysobjects 
      WHERE (type = 'U') AND name <> 'dtproperties';
   open curResetIdentity;
   fetch next from curResetIdentity into @tblName;
   while (@@Fetch_Status=0)
      set @SqlQuery = 'truncate table ' + @tblName;
      execute (@SqlQuery);
      if (@tblName <> '')
         set @SqlQuery = 'dbcc checkident(' + @tblName + ',reseed,1)';
         execute (@SqlQuery);
      fetch next from curResetIdentity into @tblName;
close curResetIdentity;
Deallocate curResetIdentity;
Note: Remember that this affects all the user tables in the database. So if you're working with a test database, where not all the tables belong to the application you're testing, this tip resets all the counters.
Jaya Nehru Kumar
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date