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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:18:45+00:00 2026-05-23T04:18:45+00:00

I have some SQL that gets a few details about a table. SELECT Column_Name,

  • 0

I have some SQL that gets a few details about a table.

SELECT Column_Name, Is_Nullable, Data_Type, Character_Maximum_Length
FROM Information_Schema.Columns
WHERE Table_Name='GenSchool'

This is working ok so far and returns a row per column in the table. What I want however, is for it to also return some Foreign Key details too. For example, GenSchool has a column SchoolType which has a FK to GenSchoolType.Code.

As well as the columns selected above, I need the query to return the FK table and column name of linked table or NULL where the column doesn’t have a FK.

This is returned from the query above.

Code            NO  nvarchar    10
CodeDescription YES nvarchar    80
Deleted         NO  bit         NULL
Type            NO  nvarchar    20

And I’d like it to return something like

Code            NO  nvarchar    10      NULL             NULL
CodeDescription YES nvarchar    80      NULL             NULL
Deleted         NO  bit         NULL    NULL             NULL
Type            NO  nvarchar    20      GenSchoolType    Code

I’ve been trying for ages using inner joins on sys tables but I’m not getting anywhere. If you need me to show what I’ve tried I can.

Thank you in advance.

  • 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-23T04:18:46+00:00Added an answer on May 23, 2026 at 4:18 am

    So I finally worked out the solution.

    with fks as ( 
        select fk.name AS 'FKName', o_p.name AS 'ParentNameTable', 
            c_p.name AS 'ParentNameColumn', o_r.name AS 'ReferencedNameTable', 
            c_r.name AS 'ReferencedNameColumn', fkc.* 
        from sys.foreign_key_columns fkc 
        inner join sys.foreign_keys fk on fk.object_id = fkc.constraint_object_id 
        inner join sys.objects o_p on o_p.object_id = fkc.parent_object_id 
        inner join sys.objects o_r on o_r.object_id = fkc.referenced_object_id 
        inner join sys.columns c_p on c_p.object_id = fkc.parent_object_id and c_p.column_id = fkc.parent_column_id 
        inner join sys.columns c_r on c_r.object_id = fkc.referenced_object_id and c_r.column_id = fkc.referenced_column_id 
    ) 
    select c.Table_Name, c.Column_Name, c.Is_Nullable, c.Data_Type,
        c.Character_Maximum_Length, COLUMNPROPERTY(OBJECT_ID(@table), 
        c.Column_Name,'IsIdentity') AS 'Is_Identity', 
        fks.FKName, fks.ReferencedNameTable, fks.ReferencedNameColumn 
    from information_schema.columns c 
    left join fks on c.Table_Name = fks.ParentNameTable and c.Column_Name = fks.ParentNameColumn 
    where c.table_name = @table
    

    This returns a row per column in the database with the following data

    1. Table Name
    2. Column Name
    3. Nullable
    4. Data Type
    5. Max Length
    6. Identity
    7. FK Name
    8. Table Referenced
    9. Column Referenced

    Feel free to use this if it meets your needs.

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

Sidebar

Related Questions

I have an a client that executes some sql and gets the results in
I have a basic VB.Net application that gets data from some websites and then
I have some SQL that does an order by case statement. It works fine.
I have an HTML textbox that contains some SQL code that I need executed.
I have some reports in SQL Server Reporting Services 2005 that I need to
I have some e-commerce code that I use often that uses Linq To SQL
I've come across some SQL queries in Oracle that contain '(+)' and I have
I have a sql database that stores some documents. A user can sign into
I'm using SQL*Plus 9.2 on Oracle 10g enterprise. I have created some scripts that
I currently have a SQL Server (Express 2005) database to hold some transaction/metadata that

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.