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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:34:15+00:00 2026-06-07T01:34:15+00:00

I have a table it is called Users Users table has UserId as a

  • 0

I have a table it is called “Users”

Users table has UserId as a primary key and it is a foreign key for other 15 tables.

Is there a query template which will select top 200 rows from users and all related tables in one shot? So i will not need to type all other 15 tables in the query.

Would Query dynamically find and bring columns from related tables?

It can use inner join for all relations between tables.

  • 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-06-07T01:34:16+00:00Added an answer on June 7, 2026 at 1:34 am

    I will admit that this is super ugly and probably needs lots more work, but it does the basic query generation based on PK/FK relations. You should probably change inner join to left join, if any of the FK tables can be missing corresponding PK records.

    declare @table varchar(50);
    set @table = 'Users';
    
    declare @pk_table varchar(50);
    declare @fk_table varchar(50);
    declare @pk_column varchar(50);
    declare @fk_column varchar(50);
    declare @curr_table varchar(50);
    set @curr_table = '';
    
    declare @sql varchar(8000);
    set @sql = 'select top 200 * from ' + @table + char(10)
    DECLARE table_cursor CURSOR FOR 
    SELECT
        K_Table = FK.TABLE_NAME,
        FK_Column = CU.COLUMN_NAME,
        PK_Table = PK.TABLE_NAME,
        PK_Column = PT.COLUMN_NAME--,
        --Constraint_Name = C.CONSTRAINT_NAME
    FROM
        INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
    INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
        ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
    INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
        ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
        ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
    INNER JOIN (
                SELECT
                    i1.TABLE_NAME,
                    i2.COLUMN_NAME
                FROM
                    INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
                INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
                    ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
                WHERE
                    i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
               ) PT
        ON PT.TABLE_NAME = PK.TABLE_NAME
    where PK.TABLE_NAME = @table   
    
    OPEN table_cursor
    FETCH NEXT FROM table_cursor INTO @fk_table, @fk_column, @pk_table, @pk_column
    
    WHILE @@FETCH_STATUS = 0
    BEGIN
        if @curr_table <> @fk_table
        begin
            -- create join
            set @sql = @sql + ' inner join ' + @fk_table + char(10)
            set @sql = @sql + ' on ' + @pk_table + '.' + @pk_column + ' = ' + @fk_table + '.' + @fk_column + char(10)
        end
        else
        begin
            -- create join on fields
            set @sql = @sql + ' and ' + @pk_table + '.' + @pk_column + ' = ' + @fk_table + '.' + @fk_column + char(10)
        end
    FETCH NEXT FROM table_cursor INTO @fk_table, @fk_column, @pk_table, @pk_column
    END
    
    CLOSE table_cursor
    DEALLOCATE table_cursor
    
    print @sql
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called user_relationship. which has two foreign keys refering back to
I have a table in database called user which has following entries. Userid FirstName
Say I have a table called Users, which contains your typical information: Id, Name,
Let's say I have a table called Users which represents registered users of a
I have a table called Users that has a column called deleted, a boolean
I have an entity called User which can have a list of other Users
I have a Windows Azure Table Called ChannelsVideos which has been denormalized to hold
I have a base table called users which holds all common information about a
I have two tables like this: Table Name: users emx | userid --------------- 1
I have a table called users and another called tickets The users table has

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.