如何在表中的列之前或之后插入列? [英] How to inser a column before or afet a column in a table ?

查看:120
本文介绍了如何在表中的列之前或之后插入列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生

我想在表格的一列之前或之后插入一个新列.这是什么查询?先生,请帮帮我.



Sashibhusan Meher

Dear Sir

I want to insert a new column before or after of a column of the table. What is the query for that ? Please help me sir.



Sashibhusan Meher

推荐答案

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

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://stackoverflow.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://stackoverflow.com/questions/2968278/t-sql-add-column-in-specific-order[^]

Hope it helps.


这篇关于如何在表中的列之前或之后插入列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆