Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6537951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:40:39+00:00 2026-05-25T10:40:39+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T10:40:39+00:00Added an answer on May 25, 2026 at 10:40 am

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there an easy way to tell if a ruby script is already running
Is there an easy way to iterate over an associative array of this structure
Is there an easy way in C# to create Ordinals for a number? For
Is there an easy way to avoid dealing with text encoding problems?
Is there an easy way to set the zoom level for a windows form
Is there an easy way to read an entire Access file (.mdb) into a
Is there an easy way of using the RegularExpressionValidator control while ignoring white space?
Is there an easy way to capitalize the first letter of a string and
Is there an easy way of finding out the host name of a machine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.