Solved

T SQL Identify Orphaned Records

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

730 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