向现有表中添加一列并在 MS SQL Server 上对其进行唯一编号 [英] Add a column to existing table and uniquely number them on MS SQL Server
本文介绍了向现有表中添加一列并在 MS SQL Server 上对其进行唯一编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向现有的遗留数据库添加一个列,并编写一个过程,通过它我可以为每条记录分配不同的值.类似于添加一个列并为其自动生成数据.
I want to add a column to an existing legacy database and write a procedure by which I can assign each record a different value. Something like adding a column and autogenerate the data for it.
比如,如果我添加一个名为ID"(数字)的新列,我想为每条记录初始化一个唯一值.因此,我的 ID 列 将包含从 1 到 1000
的记录.
我该怎么做?
Like, if I add a new column called "ID" (number) I want to then initialize a unique value to each of the records. So, my ID column will have records from say 1 to 1000
.
How do I do that?
推荐答案
这将取决于数据库,但对于 SQL Server,这可以通过以下方式实现:
This will depend on the database but for SQL Server, this could be achieved as follows:
alter table Example
add NewColumn int identity(1,1)
这篇关于向现有表中添加一列并在 MS SQL Server 上对其进行唯一编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文