关于alter table:在SQL server 的另外两列之间添加列 | 珊瑚贝

Adding column between two other columns in SQL server


您能否在不删除和重新创建表的情况下将列插入到 SQL Server 中的两个现有列之间的表中?

  • 另请参阅 stackoverflow.com/questions/1610/… 和 stackoverflow.com/questions/4402312/…
  • 采用表格设计并相应地拖动列


中等长的答案,是的(ish),但它很丑,你可能不想这样做。

请注意:此代码创建一个物理表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

ALTER TABLE MyTest ADD c int
ALTER TABLE MyTest ADD d_new int
ALTER TABLE MyTest ADD e_new int

UPDATE MyTest SET d_new = d, e_new = e

ALTER TABLE MyTest DROP COLUMN d
ALTER TABLE MyTest DROP COLUMN e

EXEC SP_RENAME ‘MyTest.d_new’, ‘d’;
EXEC SP_RENAME ‘MyTest.e_new’, ‘e’;

SELECT * FROM MyTest

DROP TABLE MyTest


简单的答案是否定的。为什么列顺序对您很重要?

  • 不是真的,我正在阅读一个要求我这样做的教程。我没有打扰,因为它在功能上毫无用处,它只是激起了我的兴趣。


是的,您可以在此处添加您关心的查询:

1
ALTER table table_name ADD column column_name(new) datatype AFTER column_name
  • 您的答案是针对 MySQL 的。他们询问有关 SQL Server 的问题。
  • 嗯,还是很感谢这个,我需要这个\\’


这在 SQL Server Management Studio (SSMS) 中是可能的。
在对象资源管理器中过滤您的表格,然后右键单击表格 > Design

enter



来源:https://www.codenong.com/5327545/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9434.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?