Note that there are some explanatory texts on larger screens.

plurals
  1. PODBCC CHECKIDENT RESEED -- is new value required?
    primarykey
    data
    text
    <p>All the documentation I read about reseeding suggests something along the lines of:</p> <ol> <li><code>SET @maxIdentityValue = (SELECT MAX(id) FROM tablename)</code></li> <li>run <code>DBCC CHECKIDENT('tablename', RESEED, @maxIdentityValue)</code></li> </ol> <p>And <em>yet</em> it appears to me that a simple <code>DBCC CHECKIDENT('tablename', RESEED)</code> is all that's needed, and it will automatically determine the correct identity value from the table without supplying a max value.</p> <p>Is there a reason (performance or otherwise) that extracting the value using <code>MAX</code> first is preferred?</p> <p>Piggyback question: the reason I need to reseed is because I'm using replication and identities keep getting set to Null each time the database replication runs. What am I doing wrong? How can I maintain the correct identity seed for each table?</p> <h2>Update (Current solution)</h2> <p>For now I'm not using the max value. This is the stored procedure I'm using (I generate it using a query on <code>sys.columns</code> and then just cutting and pasting each into a new query window. Messier, slower, less elegant, but I'm not very familiar with stored procedures and don't want to use dynamic SQL queries):</p> <pre><code>declare @seedval integer declare @maxval integer declare @newval integer set @seedval = (select ident_current('mytable')); set @maxval = (select MAX(id) from mytable); if @maxval &gt; @seedval or @seedval is NULL BEGIN print 'Need to reseed: max is ' + cast(@maxval as varchar) + ' and seed is ' + cast(@seedval as varchar) dbcc checkident('mytable', RESEED); set @newval = (select ident_current('mytable')); print 'Max is ' + cast(@maxval as varchar) + ' and seed is ' + cast(@newval as varchar) END ELSE print 'No need to reseed'; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload