Solved

T SQL Identify Orphaned Records

Posted on 2011-02-20
4
1,239 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
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 40

Expert Comment

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now