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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:23:40+00:00 2026-05-23T01:23:40+00:00

suppose i have three tables called Department ————- DeptID DeptName —— ——— 1 Accounts

  • 0

suppose i have three tables called

Department
-------------
DeptID   DeptName
------   ---------
1         Accounts
2         Sales
3         Purchase

Location
----------
LocID       LocName
------      --------
1            China
2            UK
3            USA
4            Germany

Employee
----------
EmpID     Name      Salary   LocID    DeptID
-----     -----     ------   -----    -------
1         jhon       15000      1      2
2         Sina       12500      3      1
3         Keith      17420      2      3

EMPID is PK of employee table and also employee table joined with location and department by locid and deptid.

in simple sql we can join very easily and show the data like

empid name   salary   locname   deptname.

but in my case i do not want to hard code the join by tablename.columnname.

rather i want query the system tabele and get the relationship and build the join dynamically. i am not very good in sql. so please someone help with sample as a result i can construct the join dynamically………….please help.

  • 1 1 Answer
  • 2 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-23T01:23:41+00:00Added an answer on May 23, 2026 at 1:23 am

    You can get the primary key columns of a table like this:

    select *
    from sys.identity_columns c
    where c.object_id = object_id('TableName')
    

    Then you can get the foreign key columns of a table like this:

    select *
    from sys.foreign_key_columns fkc
    inner join sys.columns c on c.object_id = fkc.parent_object_id and c.column_id = fkc.parent_column_id
    where fkc.parent_object_id = object_id('TableName')
    

    In this way you can generate the SQL for a join between the tables. This would involve a lot of string manipulation in SQL, which is not very pleasant.

    Overall, I would suggest not using dynamic SQL if possible. Is there a strong reason the SQL needs to be dynamic?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table called 'test', into which there are three columns named
Suppose I have three tables (Patients Doctors and Medicines). The Patients table has a
Suppose I have a Table1, with three Columns: ID (Primary Key, Identity), A, and
Suppose I have three projects in my sln. (1) xyz.a{Class Lib}{no reference added} (2)
Suppose I have three lists: list1 = a, c, d, r, t list2 =
suppose I have a simple container which have three element: <div> <span>hello world</span> <input
Let us suppose i have these three methods defined: int F1(int, int); int F1(float,
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
In the /data directory of solr, suppose, I have three folders named as index,
Suppose I have a string like this: one two three four five six seven

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.