SQL Query to add a new column after an existing column in SQL Server 2005

后端 未结 6 1119
后悔当初
后悔当初 2020-12-28 11:34

I need a SQL query which add a new column after an existing column, so the column will be added in a specific order.

Please suggest me if any ALTER quer

相关标签:
6条回答
  • 2020-12-28 12:07

    First add the new column to the old table through SSMStudio. Go to the database >> table >> columns. Right click on columns and choose new column. Follow the wizard. Then create the new table with the columns ordered as desired as follows:

    select * into my_new_table from (
    select old_col1, my_new_col, old_col2, old_col3
    from my_old_table 
    ) as A
    ;
    

    Then rename the tables as desired through SSMStudio. Go to the database >> table >> choose rename.

    0 讨论(0)
  • 2020-12-28 12:08

    If you want to alter order for columns in Sql server, There is no direct way to do this in SQL Server currently.

    Have a look at http://blog.sqlauthority.com/2008/04/08/sql-server-change-order-of-column-in-database-tables/

    You can change order while edit design for table.

    0 讨论(0)
  • 2020-12-28 12:16

    It's possible.

    First, just add each column the usual way (as the last column).

    Secondly, in SQL Server Management Studio Get into Tools => Options.

    Under 'Designers' Tab => 'Table and Database Designers' menu, uncheck the option 'Prevent saving changes that require table re-creation'.

    Afterwards, right click on your table and choose 'Design'. In 'Design' mode just drag the columns to order them.

    Don't forget to save.

    0 讨论(0)
  • 2020-12-28 12:24

    ALTER won't do it because column order does not matter for storage or querying

    If SQL Server, you'd have to use the SSMS Table Designer to arrange your columns, which can then generate a script which drops and recreates the table

    Edit Jun 2013

    Cross link to my answer here: Performance / Space implications when ordering SQL Server columns?

    0 讨论(0)
  • 2020-12-28 12:27

    Microsoft SQL (AFAIK) does not allow you to alter the table and add a column after a specific column. Your best bet is using Sql Server Management Studio, or play around with either dropping and re-adding the table, or creating a new table and moving the data over manually. neither are very graceful.

    MySQL does however:

    ALTER TABLE mytable
    ADD COLUMN  new_column <type>
    AFTER       existing_column
    
    0 讨论(0)
  • 2020-12-28 12:28

    It is a bad idea to select * from anything, period. This is why SSMS adds every field name, even if there are hundreds, instead of select *. It is extremely inefficient regardless of how large the table is. If you don't know what the fields are, its still more efficient to pull them out of the INFORMATION_SCHEMA database than it is to select *.

    A better query would be:

    SELECT 
     COLUMN_NAME, 
     Case 
      When DATA_TYPE In ('varchar', 'char', 'nchar', 'nvarchar', 'binary') 
      Then convert(varchar(MAX), CHARACTER_MAXIMUM_LENGTH)  
      When DATA_TYPE In ('numeric', 'int', 'smallint', 'bigint', 'tinyint') 
      Then convert(varchar(MAX), NUMERIC_PRECISION) 
      When DATA_TYPE = 'bit' 
      Then convert(varchar(MAX), 1)
      When DATA_TYPE IN ('decimal', 'float') 
      Then convert(varchar(MAX), Concat(Concat(NUMERIC_PRECISION, ', '), NUMERIC_SCALE)) 
      When DATA_TYPE IN ('date', 'datetime', 'smalldatetime', 'time', 'timestamp') 
      Then '' 
     End As DATALEN, 
     DATA_TYPE 
    FROM INFORMATION_SCHEMA.COLUMNS 
    Where 
     TABLE_NAME = ''
    
    0 讨论(0)
提交回复
热议问题