How to find orphaned records that point to different tables (paramerization required)?

Chris W

Table ( A ) has a 1-1 relation with many tables ( B, C, D, ... ) and is defined by two columns:

  • ObjectType(nvarchar(100)) // name of the other table
  • _Guid(uniqueidentifier) // record ID in the other table

Additionally, all tables contain an IsDeleted(bit) column.

The question is:

How to list all the records from (A) that point to non-existing record in B, C, D, (...) OR to the record that has IsDeleted = 1 set?

The following will not work because ObjectType must be a parameter:

SELECT ObjectType, _Guid FROM A
where 
    NOT EXISTS (
        select * from ObjectType where oid = _Guid
    )

The following will also not work:

SELECT ObjectType, _Guid FROM A
where 
    NOT EXISTS (
        exec('select * from '+ObjectType+' where oid =''' + _Guid + '''')
    )

What am I missing?

Chris W

This terrible SQL below seems to work:

DECLARE @A_tmpTable TABLE (
    idx smallint Primary Key IDENTITY(1,1)
    , ObjectType nvarchar(200)
    , _Guid nvarchar(100)
    , Oid nvarchar(100)
)
DECLARE @orphanedA_tmpTable TABLE (
    Oid nvarchar(100)
)
DECLARE @_objectType nvarchar(200)
DECLARE @_Guid nvarchar(100)
DECLARE @_oid nvarchar(100)
DECLARE @i int, @numrows int  
DECLARE @found bit

-- populate temp table
INSERT @A_tmpTable SELECT ObjectType, _Guid, Oid FROM A WHERE IsDeleted = 0
--SELECT * FROM @A_tmpTable

-- foreach the @A_tmpTable
SET @i = 1;
SET @numrows = (SELECT COUNT(*) FROM @A_tmpTable)
--SELECT @i, @numrows 
IF @numrows > 0
    WHILE (@i <= @numrows)
    BEGIN
        SET @ObjectType = (SELECT TOP 1 ObjectType FROM @A_tmpTable WHERE idx = @i);
        SET @_Guid = (SELECT TOP 1 _Guid FROM @A_tmpTable WHERE idx = @i);
        SET @_oid = (SELECT TOP 1 Oid FROM @A_tmpTable WHERE idx = @i);

        DECLARE @SQL nvarchar(max) = 'IF EXISTS (SELECT * FROM '+@ObjectType+' WHERE OID =''' + @_Guid + ''' AND IsDeleted = 0) 
                SET @found = 1;
            ELSE
                SET @found = 0;
            ';
        -- check if table record exists and save the result in the @found variable
        exec sp_executesql @SQL, N'@found bit out', @found out

        IF @found = 0
            INSERT INTO @orphanedA_tmpTable (Oid) VALUES (@_oid);


        SET @i = @i + 1
    END

SELECT * FROM A WHERE Oid IN (SELECT Oid FROM @orphanedA_tmpTable)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to drop orphaned tables in RDS

From Dev

How to drop orphaned tables in RDS

From Dev

Find different records in 2 tables mysql

From Dev

Find different records in 2 tables mysql

From Dev

related tables, how to find records if there are missing rows

From Dev

How to find records by comparing two tables in oracle

From Dev

Find matching records of two different tables in SQL Server

From Dev

TSQL: Find required missing records

From Dev

How to get records from different tables and ignoring duplicated dates in SQL?

From Dev

How to fetch records from two different tables in laravel5.2

From Dev

How to delete records from two different tables that are linked with FK? SQL

From Dev

How to find list of tables having no records in SQL server

From Dev

Sort records from different tables

From Dev

How to create list from two joined tables when foreign key is not required but still need to get all records

From Dev

How to find reason for orphaned packages in apt-get autoremove?

From Dev

how to find matching records from 3 different files in unix

From Dev

how to find the index of records with different types in series of pandas in python?

From Dev

How to find closest point (point being (x,y), in a list of different points) to a given point?

From Java

How to find different names of cities via several tables

From Dev

How to find MIN and MAX of two columns from TWO different tables?

From Dev

How to find different rows in two tables with same columns?

From Dev

Selecting all records from two different tables

From Dev

MySQL Get Different Records Bewteen Two Tables

From Dev

Counting the records of two columns from different tables

From Dev

PHP & MYSQL: Insert records in two different tables

From Dev

Insert records from two different tables into one

From Dev

mysql compare two tables to find matched records

From Dev

How to copy records from inter-linked tables to another in a different database?

From Dev

MSSQL How do I get average of four records from different tables?

Related Related

  1. 1

    How to drop orphaned tables in RDS

  2. 2

    How to drop orphaned tables in RDS

  3. 3

    Find different records in 2 tables mysql

  4. 4

    Find different records in 2 tables mysql

  5. 5

    related tables, how to find records if there are missing rows

  6. 6

    How to find records by comparing two tables in oracle

  7. 7

    Find matching records of two different tables in SQL Server

  8. 8

    TSQL: Find required missing records

  9. 9

    How to get records from different tables and ignoring duplicated dates in SQL?

  10. 10

    How to fetch records from two different tables in laravel5.2

  11. 11

    How to delete records from two different tables that are linked with FK? SQL

  12. 12

    How to find list of tables having no records in SQL server

  13. 13

    Sort records from different tables

  14. 14

    How to create list from two joined tables when foreign key is not required but still need to get all records

  15. 15

    How to find reason for orphaned packages in apt-get autoremove?

  16. 16

    how to find matching records from 3 different files in unix

  17. 17

    how to find the index of records with different types in series of pandas in python?

  18. 18

    How to find closest point (point being (x,y), in a list of different points) to a given point?

  19. 19

    How to find different names of cities via several tables

  20. 20

    How to find MIN and MAX of two columns from TWO different tables?

  21. 21

    How to find different rows in two tables with same columns?

  22. 22

    Selecting all records from two different tables

  23. 23

    MySQL Get Different Records Bewteen Two Tables

  24. 24

    Counting the records of two columns from different tables

  25. 25

    PHP & MYSQL: Insert records in two different tables

  26. 26

    Insert records from two different tables into one

  27. 27

    mysql compare two tables to find matched records

  28. 28

    How to copy records from inter-linked tables to another in a different database?

  29. 29

    MSSQL How do I get average of four records from different tables?

HotTag

Archive