是否可以在 SQL Server 中的特定序数位置向表中添加列?
例如,我们的表在每个表定义的“末尾”总是有 CreatedOn、CreatedBy、LastModifiedOn、LastModifiedBy 列? 我希望新列显示在 SSMS 中这些列的上方。
如果我正在编写所有数据库更改的脚本,是否有办法在表末尾保留此顺序?
仅供参考,我并不是想就是否应该这样做展开一场激烈的战争。 如果您想阅读有关快速退化的线程,这里有一个很好的线程:
http://www.developersdex.com/sql/message.asp?p=581&r=5014513
Is it possible to add a column to a table at a specific ordinal position in SQL Server?
For instance, our tables always have CreatedOn, CreatedBy, LastModifiedOn, LastModifiedBy columns at the "end" of each table definition? I'd like the new column to show up in SSMS above these columns.
If I am scripting all my database changes, is there a way to preserve this order at the end of the table?
FYI, I'm not trying to institute a flame war on if this should even be done. If you want to read about a thread that degenerates quickly into that, here's a good one:
http://www.developersdex.com/sql/message.asp?p=581&r=5014513
发布评论
评论(10)
您必须创建一个临时表来镜像原始表的架构,但具有所需的列顺序,然后将原始表的内容复制到临时表。 删除原来的并重命名temp。
这就是 SQL Management Studio 在幕后所做的事情。
使用架构同步工具,您可以自动生成这些脚本。
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.
进入SQL Server Management Studio,并“设计”一个现有的表。 在中间插入一列,右键单击空白区域并选择生成更改脚本...
现在查看它创建的脚本。 它基本上会创建一个具有正确列顺序的临时表,从原始表中插入数据,删除原始表,然后重命名临时表。 这可能就是您需要做的。
您可能还需要取消选中此选项以允许创建更改脚本
go into SQL Server management Studio, and "design" an existing table. Insert a column in the middle, right click in an empty area and select Generate Change Script...
Now look at the script it creates. it will basically create a temp table with the proper column order, insert the data from the original table, drop the original table, and rename the temp table. This is probably what you'll need to do.
You may also need to uncheck this option to allow creation of change scripts
答案是肯定的,技术上是可以的,但是这样做你会很头疼,而且执行和设置的时间会很长。
一:创建/复制/删除/重命名
这实际上是 SQL Server 在图形界面中所做的事情:下面是添加后单击“保存”按钮时它生成和执行的脚本的示例将新列添加到表的开头。
二:添加列/更新/删除列/重命名
此方法基本上涉及创建要添加到新列“右侧”的任何现有列的副本,将数据传输到新列列,然后删除原始列并重命名新列。 这会对您拥有的任何索引或约束造成严重破坏,因为您必须重新指向它们。 这在技术上是可行的,但在开发和执行方面同样耗时。
三:接受它
这极大地侵犯了我的秩序感……但有时,它根本不值得重新洗牌。
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.
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.
Three: Live with it
This mightily offends my sense of order ... but sometimes, it just isn't worth reshuffling.
据我所知,没有已知的方法可以更改列的顺序。 SQL Management Studio 在幕后执行 Jose Basilio 所说的操作。 如果您有一个大表,那么像这样更改列顺序是不切实际的。
您可以使用“视图”。 使用 SQL 视图,您可以使用任何您喜欢的顺序,而不会受到表列更改的影响。
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.
我正在使用 SSMS 18。我以简单的方式
I am using SSMS 18. I did in simple way
我认为简单的是添加列 ALTER TABLE table1 ADD .. 然后从 select 创建一个像 tmp_table1 这样的 tmp 表
从 table1 中选择 col1,col2,col5,col3,col4 到 tmp_table1;
然后删除table1并将tmp_table1重命名为table1,就是这样。 我希望它能帮助别人
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
将所有列选择到临时表中,然后使用所需的新列创建一个新表。 然后删除旧表,从临时表中选择所有列,并将它们插入到具有重新排序的列的新表中。 没有数据丢失。
Select all the columns into a temp table, and create a new table with the new column you want. Then drop the old table, select all the columns from the temp table, and insert them into the new table with the reordered column. No data is lost.
TFS 2013 会自动为您执行此操作。
按照您喜欢的方式将新列添加到表中,然后将更改提交到 TFS。 从那里,您可以在 Visual Studio 中打开表的 sql 文件,并手动移动 T-SQL CREATE 脚本中的列顺序。 然后,您可以使用 VS 的“工具”>“模式比较”工具来更新目标数据库。 SQL服务器> 新架构比较。 选择您的数据库项目,将您的更改作为源,并将要更新的数据库作为目标。 比较,选择表的脚本,然后更新。 VS会自动删除和添加。 您的所有数据和索引都将是安全的。
TFS 2013 will do this for you automatically.
Add the new column(s) to your table anyway you like, and then commit your changes to TFS. From there you can open the table's sql file in Visual Studio and manually move the order of the columns in the T-SQL CREATE script. Then you can update your target database by using VS's schema compare tool found under Tools > SQL Server > New Schema Comparison. Choose your Database project with your change as the source, and the database you want to update as the target. Compare, select the table's script, and Update. VS will drop and add automatically. All your data will be safe, and indexes too.
肮脏而简单。
将表导出为 csv。
在所需位置插入新数据。
删除表。
使用所需的列规格创建新表。
将 csv 中的列加载到新表中。
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.
我不确定该线程是否仍处于活动状态。 我对 MySQL 数据库进行了相同的查询。 右键单击表并选择“ALTER”自动生成以下代码。 sakila db 提供的示例并且有效。 只需找出要在其后放置新列的列并使用“AFTER”关键字
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