Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:05:12+00:00 2026-05-31T02:05:12+00:00

I have table Animal. I want to return everything from this table + one

  • 0

I have table Animal.

I want to return everything from this table + one column which denotes if record is referenced anywhere else as a foreign key.

I.E.:

Animal_Id    Name  
    1        Cat
    2        Dog
    3        Parrot

I want to return this:

AnimalId    Name     Referenced 
    1        Cat         true
    2        Dog         false
    3        Parrot      true

by ‘referenced’ I mean if a specific Animal_Id was referenced in any other table in the database as a foreign key. I think I first can query information_schema and find out what tables contain this foreign key and then use loop and dynamic sql to execute
select count(*) from eachTable where AnimalID = 1

Does anyone have a snippet on how to do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:05:12+00:00Added an answer on May 31, 2026 at 2:05 am

    This should do it:

    SELECT OO.Animal_ID, OO.Name, CASE WHEN XX.REFERENCED IS NULL THEN 'false' ELSE 'true' END Referenced
    FROM   Animal OO
           OUTER APPLY (SELECT SUM(1) REFERENCED
                        FROM   (SELECT FkAnimal_ID FROM AnimalRef1 RR WHERE RR.FkAnimal_ID = OO.Animal_ID UNION ALL
                                SELECT FkAnimal_ID FROM AnimalRef2 RR WHERE RR.FkAnimal_ID = OO.Animal_ID UNION ALL
                                SELECT FkAnimal_ID FROM AnimalRef3 RR WHERE RR.FkAnimal_ID = OO.Animal_ID) II) XX
    

    If you don’t know all the FK tables, you can use system meta-data tables to generate that collection of UNION ALL queries into a table, which you could then copy & paste into your query:

    WITH AKT AS ( SELECT f.name AS ForeignKey
                        ,OBJECT_NAME(f.parent_object_id) AS TableName
                        ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
                        ,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
                        ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
                  FROM   sys.foreign_keys AS f
                         INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
                  WHERE  f.referenced_object_id = object_id('Animal'))
    SELECT 'SELECT ' + ColumnName + ' FROM ' + TableName + ' WHERE  RR.' + ColumnName + ' = OO.' + ReferenceColumnName + ' UNION ALL'
    FROM   AKT
    

    And for the whole thing in a single query using a recursive CTE:

    DECLARE @QUERY NVARCHAR(MAX)
    
    WITH AKT AS ( SELECT ROW_NUMBER() OVER (ORDER BY f.name) RN, f.name AS ForeignKey
                        ,OBJECT_NAME(f.parent_object_id) AS TableName
                        ,COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName
                        ,SCHEMA_NAME(oo.schema_id) SchemaName
                        ,OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName
                        ,COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
                  FROM   sys.foreign_keys AS f
                         INNER JOIN sys.foreign_key_columns AS fc ON f.OBJECT_ID = fc.constraint_object_id
                         INNER JOIN sys.objects oo ON oo.object_id = fc.referenced_object_id
                  WHERE  f.referenced_object_id = object_id('Animal'))
    
        ,bs AS (SELECT AKT.RN
                      ,'SELECT ' + ColumnName + ' FROM ' + SchemaName + '.' + TableName + ' WHERE ' + ColumnName + ' = OO.' + ReferenceColumnName  SubQuery
                FROM   AKT)
        ,re AS (SELECT bs.RN, CAST(RTRIM(bs.SubQuery) AS VARCHAR(MAX)) Joined
                FROM   bs
                WHERE  bs.RN = 1
                UNION  ALL
                SELECT bs2.RN, CAST(re.Joined + ' UNION ALL ' + ISNULL(RTRIM(bs2.SubQuery), '') AS VARCHAR(MAX)) Joined
                FROM   re, bs bs2 
                WHERE  re.RN = bs2.RN - 1 )
        ,fi AS (SELECT ROW_NUMBER() OVER (ORDER BY RN DESC) RNK, Joined
                FROM   re)
    SELECT @QUERY  = 'SELECT OO.Animal_ID, OO.Name, CASE WHEN XX.REFERENCED IS NULL THEN ''No'' ELSE ''Yes'' END Referenced
    FROM   Animal OO
           OUTER APPLY (SELECT SUM(1) REFERENCED
                        FROM   (' + Joined + ') II) XX'
    FROM   fi
    WHERE  RNK = 1
    
    EXEC (@QUERY)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have a JSP page that displays records from animal table in my
Lets suppose I have a scenario with the following model: An Animal table which
I have xml column in a table and I want to parse the xml
I have table rows of data in html being filled from a CGI application.
I have table with some fields that the value will be 1 0. This
i have table structure with 3 colums (column1, column2, column3) and i want to
I have table with 3 columns A B C. I want to select *
i have table unser it i have one td with elemets inside the menu
I want to update a record in a table but based on a condition
I basically have a table with headers that I read in from a DB

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.