Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Changing the <code>IDENTITY</code> property is really a metadata only change. But to update the metadata directly requires starting the instance in single user mode and messing around with some columns in <code>sys.syscolpars</code> and is undocumented/unsupported and not something I would recommend or will give any additional details about.</p> <p>For people coming across this answer on SQL Server 2012+ by far the easiest way of achieving this result of an auto incrementing column would be to create a <code>SEQUENCE</code> object and set the <code>next value for seq</code> as the column default.</p> <p>Alternatively, or for previous versions (from 2005 onwards), the workaround posted on <a href="http://connect.microsoft.com/SQLServer/feedback/details/252226/allow-enabling-and-disabling-of-a-columns-identity-property" rel="noreferrer">this connect item</a> shows a completely supported way of doing this without any need for size of data operations using <code>ALTER TABLE...SWITCH</code>. Also blogged about on MSDN <a href="http://blogs.msdn.com/b/dfurman/archive/2010/04/20/adding-the-identity-property-to-a-column-of-an-existing-table.aspx" rel="noreferrer">here</a>. Though the code to achieve this is not very simple and there are restrictions - such as the table being changed can't be the target of a foreign key constraint.</p> <h2>Example code.</h2> <h3>Set up test table with no <code>identity</code> column.</h3> <pre><code>CREATE TABLE dbo.tblFoo ( bar INT PRIMARY KEY, filler CHAR(8000), filler2 CHAR(49) ) INSERT INTO dbo.tblFoo (bar) SELECT TOP (10000) ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM master..spt_values v1, master..spt_values v2 </code></pre> <h3>Alter it to have an <code>identity</code> column (more or less instant).</h3> <pre><code>BEGIN TRY; BEGIN TRANSACTION; /*Using DBCC CHECKIDENT('dbo.tblFoo') is slow so use dynamic SQL to set the correct seed in the table definition instead*/ DECLARE @TableScript nvarchar(max) SELECT @TableScript = ' CREATE TABLE dbo.Destination( bar INT IDENTITY(' + CAST(ISNULL(MAX(bar),0)+1 AS VARCHAR) + ',1) PRIMARY KEY, filler CHAR(8000), filler2 CHAR(49) ) ALTER TABLE dbo.tblFoo SWITCH TO dbo.Destination; ' FROM dbo.tblFoo WITH (TABLOCKX,HOLDLOCK) EXEC(@TableScript) DROP TABLE dbo.tblFoo; EXECUTE sp_rename N'dbo.Destination', N'tblFoo', 'OBJECT'; COMMIT TRANSACTION; END TRY BEGIN CATCH IF XACT_STATE() &lt;&gt; 0 ROLLBACK TRANSACTION; PRINT ERROR_MESSAGE(); END CATCH; </code></pre> <h3>Test the result.</h3> <pre><code>INSERT INTO dbo.tblFoo (filler,filler2) OUTPUT inserted.* VALUES ('foo','bar') </code></pre> <h3>Gives</h3> <pre><code>bar filler filler2 ----------- --------- --------- 10001 foo bar </code></pre> <h3>Clean up</h3> <pre><code>DROP TABLE dbo.tblFoo </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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