且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将自动增量添加到新添加的列,该列为空,其行中包含相应的数据

更新时间:2023-09-24 17:31:58

是的,它是。



 更新 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.