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

后端 未结 9 934
北海茫月
北海茫月 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.

    0 讨论(0)
  • 2020-11-27 20:26

    Dirty and simple.
    Export table to csv.
    Insert new data at desired position.
    Drop table.
    Create new table with desired column specifications.
    Load columns from csv to new table.

    0 讨论(0)
  • 2020-11-27 20:27

    You have to create a temp table that mirrors the original table's schema but with the column order that you want, then copy the contents of the original to temp. Delete the original and rename the temp.

    This is what SQL Management Studio does behind the scenes.

    With a schema sync tool, you can generate these scripts automatically.

    0 讨论(0)
  • 2020-11-27 20:27

    What i think is simple is to add the column ALTER TABLE table1 ADD .. and then create a tmp table like tmp_table1 from the select like SELECT col1,col2,col5,col3,col4 into tmp_table1 from table1; and then drop table1 and rename the tmp_table1 to table1, that is it. I hope it will help someone

    0 讨论(0)
  • 2020-11-27 20:33

    I am not sure if the thread is still active. I was having the same query with MySQL database. Right clicking the table and selecting 'ALTER' auto generated the below code. Sample provided from sakila db and it worked. Just find out the column after which you want to place your new column and use 'AFTER' keyword

    ALTER TABLE `sakila`.`actor` 
    CHANGE COLUMN `middle_name` `middle_name` VARCHAR(50) NULL DEFAULT NULL AFTER `first_name`; 
    
    0 讨论(0)
  • 2020-11-27 20:34

    To my knowledge there is no known method to change the order of the column. Behind the scenes SQL Management Studio does what Jose Basilio said. And if you have a big table then it is impractical to change the column orders like this way.

    You can use a "view". With SQL views you can use any order you like without getting affected by the table column changes.

    0 讨论(0)
提交回复
热议问题