I want to update my temp table records. But my existing Temp table does not have any unique column. So I need to append Identity column and update all the records based on that Identity column.
For example, If my temp table has 1000 records without any unique column values. I need to number all these 1000 records and update values.
while(@count < identity_value)
begin
update #temp
Name = 'Gold'
where identity = @count
@count = @count+1
End
I can Alter table option but in my case records are already inserted into my temp table. So I need to loop thorugh it by adding Identity column.
There is no need to do an UPDATE
. The identity column is going to be populated when it is created. All you need is:
ALTER TABLE #temp
ADD Id INT Identity(1, 1)
GO
Id
field will be populated and it will hold values 1, 2, ..., 1000
.
You don't need loops. See this simplified example:
CREATE TABLE #temp
(
Name varchar(10)
)
INSERT #temp VALUES ('A'),('B')
--Add identity column
ALTER TABLE #temp ADD ID int IDENTITY
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.