Solved

T SQL Identify Orphaned Records

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

919 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

12 Experts available now in Live!

Get 1:1 Help Now