If the primary key id is a integer based identity column, if not create a temp table with same structure and additional identity column, and use the identity column instead of primary key id
UPDATE TableName SET ErrorDescription = 'Duplicate record' WHERE PrimaryKeyId > (SELECT MIN(PrimaryKeyId) FROM TableName CheckDuplicate WHERE (CheckDuplicate.Code = TableName.Code OR CheckDuplicate.Name = TableName.Name)) AND ErrorDescription IS NULL
If records were
| PrimaryKeyId | Code | Name | ErrorDescription |
| 1 | CD1 | Name 1 | |
| 2 | CD2 | Name 2 | |
| 3 | CD1 | Name 3 | |
| 4 | CD4 | Name 1 |
They become
| PrimaryKeyId | Code | Name | ErrorDescription |
| 1 | CD1 | Name 1 | |
| 2 | CD2 | Name 2 | |
| 3 | CD1 | Name 3 | Duplicate record |
| 4 | CD4 | Name 1 | Duplicate record |
No comments:
Post a Comment