Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 can not update duplicate records

Author  Topic 

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-04-17 : 12:45:31
Why i can not update the duplicate records?
the output will be dup='Y' for Id: 1, and 2

declare @tbl1 table
(ID INT,
Dup nvarchar(1)
)

INSERT INTO @tbl1
SELECT 1, 'Y'

INSERT INTO @tbl1
SELECT 1, null

INSERT INTO @tbl1
SELECT 1, null

INSERT INTO @tbl1
SELECT 2, 'Y'

INSERT INTO @tbl1
SELECT 2, null

INSERT INTO @tbl1
SELECT 2, null

INSERT INTO @tbl1
SELECT 3, null

INSERT INTO @tbl1
SELECT 4, null


UPDATE a
SET a.Dup = 'Y'
FROM (
SELECT ID
FROM @tbl1
WHERE Dup = 'Y'
Group By ID
) d
join @tbl1 a
on a.ID = d.ID
WHERE a.Dup!= 'Y'

select ID,Dup from @tbl1

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-04-17 : 13:15:57
may be u need this?

UPDATE a
SET a.Dup = 'Y'
FROM (
SELECT ID,count(ID) as cnt
FROM @tbl1
Group By ID
) d
join @tbl1 a
on a.ID = d.ID
WHERE d.cnt > 1
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-04-17 : 13:34:35
thank vijayisonly,

if i have more data, it will update Id =4 too.

declare @tbl1 table
(ID INT,
Dup nvarchar(1)
)

INSERT INTO @tbl1
SELECT 1, 'Y'

INSERT INTO @tbl1
SELECT 1, null

INSERT INTO @tbl1
SELECT 1, null

INSERT INTO @tbl1
SELECT 2, 'Y'

INSERT INTO @tbl1
SELECT 2, null

INSERT INTO @tbl1
SELECT 2, null

INSERT INTO @tbl1
SELECT 3, null

INSERT INTO @tbl1
SELECT 4, null

INSERT INTO @tbl1
SELECT 4, null

INSERT INTO @tbl1
SELECT 4, null

INSERT INTO @tbl1
SELECT 4, null



UPDATE a
SET a.Dup = 'Y'
FROM (
SELECT ID,count(ID) as cnt
FROM @tbl1
Group By ID
) d
join @tbl1 a
on a.ID = d.ID
WHERE d.cnt > 1

select ID,Dup from @tbl1
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-04-17 : 13:36:43
If you have 3 records for ID = 4 ....Isn't that considered duplicate?

Are you saying we should be updating only if Dup = 'Y' for one of the rows?
If that is the case...try this

UPDATE a
SET a.Dup = 'Y'
FROM (
SELECT ID,Dup
FROM @tbl1
where Dup = 'Y'
) d
join @tbl1 a
on a.ID = d.ID
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-04-17 : 13:49:54
Another alternative:
UPDATE @tbl1
SET Dup = 'Y'
WHERE ID IN (SELECT DISTINCT ID FROM @tbl1 WHERE Dup = 'Y')
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-17 : 13:55:58
[code]
UPDATE t
SET t.Dup='Y'
FROM @tbl1 t
JOIN (SELECT ID FROM @tbl1 GROUP BY ID HAVING COUNT(CASE WHEN Dup='Y' THEN 1 ELSE 0 END) >0 ) t1
ON t1.ID=t.ID
WHERE t.Dup IS NULL
[/code]
Go to Top of Page

rudba
Constraint Violating Yak Guru

415 Posts

Posted - 2009-04-17 : 15:12:53
thanks guys,
it works.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-17 : 15:15:02
welcome
Go to Top of Page
   

- Advertisement -