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 6630883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:28:12+00:00 2026-05-25T22:28:12+00:00

If I have a table, TableA: Id 1 2 3 … And two other

  • 0

If I have a table, TableA:

Id
1
2
3
...

And two other tables:

TableB:

Id, TableAId
1   1
2   1

TableC:

Id, TableAId
1,  1
2,  2

Where TableAId is a FK relationship with TableA.Id.

How do I determine that TableA, Id 1, has three rows pointing to it? And that TableA, Id 2 has one row pointing to it? And more specifically, how do I identify what those rows are? (their table name and Id)

  • 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-25T22:28:13+00:00Added an answer on May 25, 2026 at 10:28 pm

    You can use the INFORMATION_SCHEMA views to generate select statements to display the rows in question. I have only tested this against the tables provided in the question, but it could be expanded to work in cases where the keys are multiple columns.

    declare @table_schema nvarchar(50) = 'dbo',
            @table_name nvarchar(50) = 'TableA',
            @id int = 1
    
    select fk_col.TABLE_SCHEMA, fk_col.TABLE_NAME, fk_col.COLUMN_NAME, 
        'select * from ' + fk_col.TABLE_SCHEMA + '.' + fk_col.TABLE_NAME + ' t1 '
            + ' inner join ' + @table_schema + '.' + @table_name + ' t2 '
            + ' on t1.' + fk_col.COLUMN_NAME + ' = t2.' + pk_col.COLUMN_NAME
            + ' where t2.' + pk_col.COLUMN_NAME + ' = ' + cast(@id as nvarchar)
    
    from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk
    
        join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE pk_col
            on pk.CONSTRAINT_SCHEMA = pk_col.CONSTRAINT_SCHEMA
            and pk.CONSTRAINT_NAME = pk_col.CONSTRAINT_NAME
    
        join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS fk 
            on pk.CONSTRAINT_SCHEMA = fk.UNIQUE_CONSTRAINT_SCHEMA 
            and pk.CONSTRAINT_NAME = fk.UNIQUE_CONSTRAINT_NAME
    
        join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE fk_col
            on fk_col.CONSTRAINT_SCHEMA = fk.CONSTRAINT_SCHEMA
            and fk_col.CONSTRAINT_NAME = fk.CONSTRAINT_NAME
    
    where pk.TABLE_SCHEMA = @table_schema 
        and pk.TABLE_NAME = @table_name
        and pk.CONSTRAINT_TYPE = 'PRIMARY KEY'
    

    The select statements generated:

    select * from dbo.TableB t1  inner join dbo.TableA t2  on t1.TableAId = t2.Id where t2.Id = 1
    select * from dbo.TableC t1  inner join dbo.TableA t2  on t1.TableAId = t2.Id where t2.Id = 1
    

    and the query results:

    Id          TableAId    Id
    ----------- ----------- -----------
    1           1           1
    2           1           1
    
    Id          TableAId    Id
    ----------- ----------- -----------
    1           1           1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have three tables : TableA (key, value) TableB (key, value) TableC (key,
I have a table (TableB) that has a foreign key relationship with a parent
I have TableA in a many-to-many relationship with TableC via TableB. That is, TableA
I have two tables that are indirectly related by another table TableA - ID,
I have a table called TableA whch has a foreign key from TableB and
I have two databases A and B. A has table tableA with columnA similarily
I have three tables, table one (tableA) containing users data like name and email,
I have a table in my database (TableA) that has a column (TableA.Column1) that
I have two tables, tableA and tableB. I want to set a trigger. Once
I have a main Table, with several child tables. TableA and TableAChild1 and TableAChild2.

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.