Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server how to drop identity from a column

Tags:

Is there an easy way to remove an identity from a table in SQL Server 2005?

When I use Management Studio, it generates a script that creates a mirror table without the identity, copies the data, drops the table, then renames the mirror table, etc. This script has 5231 lines in it because this table/column have many FK relations.

I'd feel much more comfortable running a simple alter/drop. Any ideas?

EDIT
I think I'm just going to go with the 5,231 line script from Enterprise Manager. However, I'm going to break it up into smaller parts which I can run and control better. This table "behaves" strange, if you try to delete 1 row (even one you just inserted, which is not in any other FK table), you get this error:

delete MyTable where MyPrimaryKey=1234  

Msg 8621, Level 17, State 2, Line 1
    The query processor ran out of stack space during query optimization. Please simplify the query.

No doubt, all the FKs. We will halt all access to our application and run in single user mode when we make these schema and related application changes. However, we need this to run fast, and I need an idea of how long it will take. I guess that I'll just have to test, test, test.

like image 878
KM. Avatar asked Mar 31 '09 19:03

KM.


People also ask

How remove identity property from an existing column in SQL Server?

Execute an UPDATE statement to set the value of the new column to the value of the identity column in each row. Drop the identity column. Rename the new column to replace the original identity column.

How do I change the identity in an existing column in SQL Server?

You can't alter the existing columns for identity. You have 2 options, Create a new table with identity & drop the existing table. Create a new column with identity & drop the existing column.

Can you update identity column?

You can update the value in an identity column to a specified value or have the system generate a new value.


2 Answers

If you are on SQL Server 2005 or later, you can do this as a simple metadata change (NB: doesn't require an edition supporting partitioning as I originally stated).

Example code pilfered shamelessly from the workaround by Paul White on this Microsoft Connect Item.

USE tempdb;
GO
-- A table with an identity column
CREATE TABLE dbo.Source 
(row_id INTEGER IDENTITY PRIMARY KEY NOT NULL, data SQL_VARIANT NULL);
GO
-- Some sample data
INSERT dbo.Source (data)
VALUES (CONVERT(SQL_VARIANT, 4)),
        (CONVERT(SQL_VARIANT, 'X')),
        (CONVERT(SQL_VARIANT, {d '2009-11-07'})),
        (CONVERT(SQL_VARIANT, N'áéíóú'));
GO
-- Remove the identity property
BEGIN TRY;
    -- All or nothing
    BEGIN TRANSACTION;

    -- A table with the same structure as the one with the identity column,
    -- but without the identity property
    CREATE TABLE dbo.Destination 
    (row_id INTEGER PRIMARY KEY NOT NULL, data SQL_VARIANT NULL);

    -- Metadata switch
    ALTER TABLE dbo.Source SWITCH TO dbo.Destination;

    -- Drop the old object, which now contains no data
    DROP TABLE dbo.Source;

    -- Rename the new object to make it look like the old one
    EXECUTE sp_rename N'dbo.Destination', N'Source', 'OBJECT';

    -- Success
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH
    -- Bugger!
    IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
    PRINT ERROR_MESSAGE();
END CATCH;
GO

-- Test the the identity property has indeed gone
INSERT dbo.Source (row_id, data)
VALUES (5, CONVERT(SQL_VARIANT, N'This works!'))

SELECT row_id,
        data
FROM    dbo.Source;
GO

-- Tidy up
DROP TABLE dbo.Source;
like image 70
Martin Smith Avatar answered Sep 20 '22 04:09

Martin Smith


I don't believe you can directly drop the IDENTITY part of the column. Your best bet is probably to:

  • add another non-identity column to the table
  • copy the identity values to that column
  • drop the original identity column
  • rename the new column to replace the original column

If the identity column is part of a key or other constraint, you will need to drop those constraints and re-create them after the above operations are complete.

like image 45
DCNYAM Avatar answered Sep 19 '22 04:09

DCNYAM