且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在表中的列之前或之后插入列?

更新时间:2023-11-30 20:27:16

根据我的理解,您想更改表
如果正确,请在此处检查

http://www.w3schools.com/sql/sql_alter.asp [ http://sqlserverplanet.com/sql/alter-table-add-column/ [ ^ ]
According to my understanding you want to alter table
if it is correct then check here

http://www.w3schools.com/sql/sql_alter.asp[^]

http://sqlserverplanet.com/sql/alter-table-add-column/[^]


SQL Server不允许您在查询的特定点插入一列(与MySql不同)-它们总是插入到末尾.唯一的方法是:

1)创建一个具有所需结构的新表
2)将数据从原始表复制到该新表中
3)删除原始表
4)将新表重命名为原始表名

在末尾插入一列很容易:
SQL Server does not give you an option to insert a column at a specific point with a query (unlike MySql) - they are always inserted at the end. The only way to do it is:

1) Create a of new table that has got the required structure
2) Copy the data from the original table into this new table
3) Delete the original table
4) Rename the new table to the original table name

Inserting a column at the end is easy:
ALTER TABLE myTable ADD newColumn INT



老实说,没有充分的理由在特定的列上插入一列:除非您在SELECT语句中指定所有列"(而且无论如何也不要这样做),否则请指定返回列的顺序,因此数据库中的顺序是无关紧要的.



To be honest, there is no good reason for inserting a column at a specific column: Unless you specify "all columns" in your SELECT statement (and you shouldn''t really do that anyway) you specify the order in which columns are returned, so the order in the DB is irrelevant.


您不能在不重新创建表的情况下执行此操作.

为了证明这种打开的SSMS,请转到表的设计视图,并在各列之间插入一行并定义其类型,然后默认单击位于工具栏最左侧的Generate Change Script按钮.
您可以在脚本中看到没有这样的插入列命令,并且为此目的重新创建了表.

还有一点是,您可以在select 语句以及insert语句中对列进行重新排序.

另一个遇到您问题的人:
> http://***.com/questions/2968278/t-sql-add-按特定顺序排列的列 [ ^ ]

希望对您有所帮助.
You can not do it without recreation of the table.

For the proof of this open SSMS and go to design view of a table and insert a row between columns and define its type then click the Generate Change Script button which is located in the left most side of the toolbar by default.
You can see in the script that there is no such insert column command and table was recreated for this purpose.

And another point is that you can reorder columns in select statements and also in insert statements.

Another person that had your problem :
http://***.com/questions/2968278/t-sql-add-column-in-specific-order[^]

Hope it helps.