Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T SQL Identify Orphaned Records

Posted on 2011-02-20
4
Medium Priority
?
1,300 Views
Last Modified: 2012-05-11
Hi,

I have asslight problem with a database table and query I could do with some help with.

I have a table 'icomponent' with the following fields (shortened to what is important for now)

icomponent_id
ilocation_id
icomponent_parent_id

'icomponent_id' is an Identity field and the primary key on the table.  
'icomponent_parent_id' is a reference back to the icomponent table to allow a heirachy to be defined within the table.  It may be null for a top level component or is the ID (icomponent_id) of the parent item.

I already have a FK constraint to ensure that the icomponent_id to icomponent_parent_id link is valid.  

My problem is that when displaying these items in the database they are always displayed by their location (ilocation_id) meaning that it is possible and it has happened that two components linked to each other via the icomponent_parent_id field get in different locations which causes the child to not be displayed as its parent is never displayed for the location in which the child has been set.  Hope that makes sense so far.

I have tried to create a SQL statement to detect these instances as below:-

select * from icomponent T1
where T1.icomponent_parent_id <> null
      and T1.icomponent_parent_id NOT IN
      (SELECT icomponent_id FROM icomponent T2 WHERE T1.ilocation_id = T2.ilocation_id)

but I guess I have made an error here - hopefully you can see what I am trying to achieve.

I have two questions :- First how can I modify the query above so that I can find all components whose parent component (if there is one) is NOT in the same location that they are.

Secondly, is it possible to change the foreign key to prevent this from ever happening - if there is how could I re-locate the parent and all of its children in one operation so that I didn't break the FK?
0
Comment
Question by:ChrisMDrew
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 16

Accepted Solution

by:
BurnieP earned 1200 total points
ID: 34938296
Hi, try this :

select * from icomponent T1 join icomponent T2 on T1.icomponent_id = T2.icomponent_parent_id where T1.ilocation_id <> T2.ilocation_id
0
 
LVL 41

Expert Comment

by:Sharath
ID: 34938428
Can you provide some sample data with expected result?
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 800 total points
ID: 34938536
first point has been answered.

second option could be done via trigger to reject a insert/update of the child if the location is not the same, and or to update the child's location whenever the parent's location changes automatically
or
keep the locationid = null to indicate that the location to consider is the one of the parent
0
 

Author Comment

by:ChrisMDrew
ID: 34938947
Thanks for that - I realised that the problem with my query was the '<> null' - it should of course have been not null which made my query work - the suggested query is a lot better though!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question