将自动增量添加到新添加的列,该列为空,其行中包含相应的数据 [英] Add auto increment to newly added column which is null with respective datas in its row

查看:68
本文介绍了将自动增量添加到新添加的列,该列为空,其行中包含相应的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个11行变量表,现在我需要在id列中设置为主键,我想编号或自动增加11行的id列



例如:

id姓名地址

null ab

null cd





11条记录



i想要那样的输出

id name address

1 ab

2 cd







11 zf



是否有任何查询要做...

解决方案

是的,它是。



 更新 dst 
SET dst.id = src.id
FROM YourTableName AS dst INNER JOIN
SELE CT ROW_NUMBER() OVER ORDER BY [name]) AS id,[name],[address]
AS src ON dst。[name] = src。[name] AND dst。[address] = src。[地址]





注意: Insted使用查询,设置 IDENTITY [ ^ ] id 字段的属性。


Alter Table RegistrationForm

添加Id_new Int身份(1,1)

Go



Alter Table RegistrationForm Drop Column id

Go

Exec sp_rename'RegistryForm.Id_new','id','Column'

GO



我得到了代码......感谢所有答案。


I have a table with 11 rows variable now i am need in id column and set as primary key and i want to number or auto increment the id column for 11 rows

eg :
id name address
null a b
null c d
.
.
11 records

i want ot output like that
id name address
1 a b
2 c d
.
.
.
11 z f

is there any query to do it...

解决方案

Yes, it is.

UPDATE dst
    SET dst.id = src.id
FROM YourTableName AS dst INNER JOIN (
    SELECT ROW_NUMBER() OVER(ORDER BY [name]) AS id, [name], [address]
    ) AS src ON dst.[name] = src.[name] AND dst.[address] = src.[address]



Note: Insted using query, set IDENTITY[^] property for id field.


Alter Table RegistrationForm
Add Id_new Int Identity(1, 1)
Go

Alter Table RegistrationForm Drop Column id
Go
Exec sp_rename 'RegistrationForm.Id_new', 'id', 'Column'
GO

I got the code...Thanks for the all the answer.


这篇关于将自动增量添加到新添加的列,该列为空,其行中包含相应的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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