sql server 2008 - 更改列位置 [英] sql server 2008 - change column position
问题描述
我有一个包含a,b,c,d等列的表我需要在列a之前添加标识列,即as表格的第一列。
请尽快帮助..
Hi,
I have a table with columns a,b,c,d, etc I need to add identity column before the column "a" i.e. as a first column of a table.
Pleas help asap..
推荐答案
试试这个:
Try this:
ALTER TABLE MyTable
ADD ID numeric NOT NULL IDENTITY (1, 1)
你不能在特定的位置插入一列 - 如果你需要的话(除非你用SELECT * FROM选择所有列,否则你不应该这样做...而不是按照良好做法推荐列出名称,然后您必须创建一个新的临时表,其中的列按您希望的方式排序,包括新的列并复制数据。然后,您可以删除旧表,并重命名新表。
You can''t insert a column in a particular position - if you need that (and you shouldn''t unless you are selecting all columns with "SELECT * FROM ..." instead of listing the names as is recommended for good practice) then you will have to create a new temporary table with the columns ordered the way you want including the new column and copy the data over. You can then delete the old table, and rename the new.
如果datatable包含标识列,请使用 SELECT [ ^ ]声明:
If datatable contains identity column, use SELECT[^] statement:
SELECT IDColumn AS ID, a, b, c, d
FROM YourTable
示例: http://msdn.microsoft.com/en-us/library/ms187731.aspx [ ^ ]
如果datatable不包含标识列,请使用 ALTER TABLE [ ^ ]添加列的语句。
Examples: http://msdn.microsoft.com/en-us/library/ms187731.aspx[^]
If datatable does not contains identity column, use ALTER TABLE[^] statement to add a column.
这篇关于sql server 2008 - 更改列位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!