Azzeramento dei campi identity seed in SQL Server

By cillo at January 06, 2007 02:02
Filed Under: Microsoft, SQL

Questa procedura permette di azzerare i campi identity nelle tabelle sql; è una operazione utile quando ad esempio si fanno delle prove sul database e poi si vuole avere la tabella "pulita", senza cioè avere "buchi" nella numerazione delle colonne che normalmente sono gli ID.
Si utilizza la funzione:
TRUNCATE nome_tabella

Se però esistono vincoli esterni di relazione con altre tabelle non è possibile utilizzare la procedura segnalata; bisogna invece utilizzare la seguente:
DBCC CHECKIDENT ('nome_tabella', RESEED, 1)
Nel caso in cui si voglia riportare l'identity a 1.

DBCC CHECKIDENT ('nome_tabella', RESEED)
In questo caso invece l'identity viene riportata al valore più alto presente nella tabella.

Comments are closed

About Me

My work experience began in the second half of the nineties, designing and developing mainly internet-based solutions for businesses.
As a natural evolution, I started focusing on the architectural aspect of IT systems.
I have been a system administrator and IT manager for years now, and I take care of designing, implementing and maintaining customer IT infrastructures.

If you want to know more, please take a look on

Recent comments

Comment RSS

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008