Wednesday 7 December 2016

Update Second Row if two rows has same values



Here i want to Update the Second repeated digit Like (3 as 6) without any Unique column.
As per above given attach.

Query :

create table #temp(Item int)
insert into #temp values(3)
insert into #temp values(5)
insert into #temp values(Null)
insert into #temp values(8)
insert into #temp values(Null)
insert into #temp values(3)

select * from #temp


WITH MyTable AS
(
    SELECT Item,
    ROW_NUMBER() OVER ( ORDER BY Item ) AS 'RowNumber'
    FROM #temp

update MyTable set item=6 where RowNumber=3

--SELECT RowNumber,item     
--FROM MyTable 
--WHERE Item = 3

select * from #temp

No comments:

Post a Comment