Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Difference Between Queries (Finding Orphans)

Author  Topic 

mkdlmr
Starting Member

21 Posts

Posted - 2014-01-17 : 16:03:30
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

1974 Posts

Posted - 2014-01-17 : 17:50:42
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

52326 Posts

Posted - 2014-01-18 : 02:21:55
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 - 2014-01-21 : 11:03:31
Thanks guys! I'm up and working!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-01-22 : 06:37:31
cool

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

- Advertisement -