SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Difference Between Queries (Finding Orphans)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mkdlmr
Starting Member

21 Posts

Posted - 01/17/2014 :  16:03:30  Show Profile  Reply with Quote
Hi All,

I have a table with organizations. Within that table there are Organization_IDs and Organization_Parent_IDs. These two values should correspond to each-other or the Organization_Parent_IDs value should be null. Can you please tell me how I can go about searching for orphaned organizations?

Thanks,
Mark

chadmat
The Chadinator

USA
1974 Posts

Posted - 01/17/2014 :  17:50:42  Show Profile  Visit chadmat's Homepage  Reply with Quote
SELECT *
FROM <Table>
WHERE (Organization_Parent_ID NOT IN (SELECT DISTINCT Organization_ID FROM <table>))
AND Organization_Parent_ID IS NOT NULL

HTH,


-Chad
Microsoft Certified Master SQL Server 2008
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/18/2014 :  02:21:55  Show Profile  Reply with Quote
quote:
Originally posted by mkdlmr

Hi All,

I have a table with organizations. Within that table there are Organization_IDs and Organization_Parent_IDs. These two values should correspond to each-other or the Organization_Parent_IDs value should be null. Can you please tell me how I can go about searching for orphaned organizations?

Thanks,
Mark



SELECT *
FROM Organizations o
WHERE NOT EXISTS (SELECT 1
FROM Organizations
WHERE Organization_IDs = o.Organization_Parent_IDs 
)
AND Organization_Parent_IDs IS NOT NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

mkdlmr
Starting Member

21 Posts

Posted - 01/21/2014 :  11:03:31  Show Profile  Reply with Quote
Thanks guys! I'm up and working!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 01/22/2014 :  06:37:31  Show Profile  Reply with Quote
cool

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000