Solved

T SQL Identify Orphaned Records

Posted on 2011-02-20
4
1,289 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 300 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

617 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