adspace


How can we remove orphan records from a table?

Answer Posted / Jitendra Singh Yadav

In SQL Server, you can use the DELETE statement with JOIN to delete orphan records. Here's an example using self-join:

```sql
DELETE A
FROM TableA AS A
JOIN (
SELECT ParentID, MAX(ID) as MaxID
FROM TableA
GROUP BY ParentID
) AS B ON A.ParentID = B.ParentID AND A.ID < B.MaxID
```

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is normalization and what are the advantages of it?

1054


How to enter binary string literals in ms sql server?

1244


What is temporary table in sql server? Why we use temp table?

1020


How raid can influence database performance?

1116


Can you index views?

954


How do I find the sql server version?

1102


What are the source of constraints?

1008


what is spatial nonclustered index

1057


Does view occupy space?

1019


Is it possible to have clustered index on separate drive from original table location?

1039


What is a scheduled job or what is a scheduled task?

1039


What is sql or structured query language?

1227


What is the difference between upgrade and migration in sql server?

1184


what is the difference between openrowset and openquery?

1194


Why use identity in sql server?

1199