Add a new table column to specific ordinal position in Microsoft SQL Server

后端 未结 9 965
北海茫月
北海茫月 2020-11-27 20:16

Is it possible to add a column to a table at a specific ordinal position in Microsoft SQL Server?

For instance, our tables always have CreatedOn, CreatedBy, LastModi

9条回答
  •  粉色の甜心
    2020-11-27 20:21

    The answer is yes, it is technically possible, but you will have a headache doing so and it will take a long time to execute and set up.

    One: Create/Copy/Drop/Rename

    This is actually what SQL Server is doing in the graphical interface: here's an example of the script it is generating and executing when you click the 'save' button after adding a new column to the beginning of a table.

    /* To prevent any potential data loss issues, you should review this script in detail before running it outside the context of the database designer.*/
    BEGIN TRANSACTION
    SET QUOTED_IDENTIFIER ON
    SET ARITHABORT ON
    SET NUMERIC_ROUNDABORT OFF
    SET CONCAT_NULL_YIELDS_NULL ON
    SET ANSI_NULLS ON
    SET ANSI_PADDING ON
    SET ANSI_WARNINGS ON
    COMMIT
    BEGIN TRANSACTION
    GO
    CREATE TABLE dbo.Tmp_SomeTable
        (
        MyNewColumn int NOT NULL,
        OriginalIntColumn int NULL,
        OriginalVarcharColumn varchar(100) NULL
        )  ON [PRIMARY]
         TEXTIMAGE_ON [PRIMARY]
    GO
    ALTER TABLE dbo.Tmp_SomeTable SET (LOCK_ESCALATION = TABLE)
    GO
    SET IDENTITY_INSERT dbo.Tmp_SomeTable ON
    GO
    IF EXISTS(SELECT * FROM dbo.SomeTable)
         EXEC('INSERT INTO dbo.Tmp_SomeTable (OriginalIntColumn, OriginalVarcharColumn FROM dbo.SomeTable WITH (HOLDLOCK TABLOCKX)')
    GO
    SET IDENTITY_INSERT dbo.Tmp_SomeTable OFF
    GO
    DROP TABLE dbo.SomeTable
    GO
    EXECUTE sp_rename N'dbo.Tmp_SomeTable', N'SomeTable', 'OBJECT' 
    GO
    
    GO
    COMMIT
    

    Two: ADD COLUMN / UPDATE / DROP COLUMN / RENAME

    This method basically involves creating a copy of any existing columns that you want to add to the 'right' of your new column, transferring the data to the new column, then dropping the originals and renaming the new ones. This will play havoc with any indexes or constraints you have, since you have to repoint them. It's technically possible, but again time-consuming both in terms of development and execution.

    CREATE TABLE MyTest (a int, b int, d int, e int)
    
    INSERT INTO MyTest (a,b,d,e) VALUES(1,2,4,5)
    
    SELECT * FROM MyTest -- your current table
    
    ALTER TABLE MyTest ADD c int -- add a new column
    ALTER TABLE MyTest ADD d_new int -- create copies of the existing columns you want to move
    ALTER TABLE MyTest ADD e_new int
    
    UPDATE MyTest SET d_new = d, e_new = e -- transfer data to the new columns
    
    ALTER TABLE MyTest DROP COLUMN d -- remove the originals
    ALTER TABLE MyTest DROP COLUMN e
    
    EXEC SP_RENAME 'MyTest.d_new', 'd'; -- rename the new columns
    EXEC SP_RENAME 'MyTest.e_new', 'e';
    
    SELECT * FROM MyTest 
    
    DROP TABLE MyTest -- clean up the sample
    

    Three: Live with it

    This mightily offends my sense of order ... but sometimes, it just isn't worth reshuffling.

提交回复
热议问题