Solved

I also wanted the query to show the column type  - not sure how to do that? please help - thanks

Posted on 2011-03-17
4
206 Views
Last Modified: 2012-06-22
select object_name(col.object_id) as [table],  col_name(col.object_id, col.column_id) as [column], object_name(fk.referenced_object_id) as [referenced table], col_name(fk.referenced_object_id, fk.referenced_column_id) as [foreign key] from sys.columns col join sys.objects obj on obj.object_id = col.object_id left join sys.foreign_key_columns fk on col.object_id = fk.parent_object_id  and col.column_id = fk.parent_column_id  where obj.type = 'U'
0
Comment
Question by:goodk
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 150 total points
ID: 35162681
SELECT table_name=sysobjects.name,
         column_name=syscolumns.name,
         datatype=systypes.name,
         length=syscolumns.length
    FROM sysobjects
    JOIN syscolumns ON sysobjects.id = syscolumns.id
    JOIN systypes ON syscolumns.xtype=systypes.xtype
   WHERE sysobjects.xtype='U'
ORDER BY sysobjects.name,syscolumns.colid
0
 
LVL 40

Accepted Solution

by:
Sharath earned 350 total points
ID: 35162916
Are you looking for this?
SELECT OBJECT_NAME(col.object_id)                                AS [table], 
       COL_NAME(col.object_id,col.column_id)                     AS [column], 
       t.name                                                    AS [datatype], 
       OBJECT_NAME(fk.referenced_object_id)                      AS [referenced table], 
       COL_NAME(fk.referenced_object_id,fk.referenced_column_id) AS [foreign key] 
  FROM sys.columns col 
       JOIN sys.objects obj 
         ON obj.object_id = col.object_id 
       JOIN sys.types t 
         ON col.system_type_id = t.system_type_id 
       LEFT JOIN sys.foreign_key_columns fk 
         ON col.object_id = fk.parent_object_id 
            AND col.column_id = fk.parent_column_id 
 WHERE obj.TYPE = 'U'

Open in new window

0
 

Author Comment

by:goodk
ID: 35166221
Sharath_123: Hi thanks, yeah this is what I was looking for,  please also, add datatype length
and where clause to select certing table, column

thanks a lot - really appreciate your help.
0
 

Author Closing Comment

by:goodk
ID: 35166868
Great help - really appreciated - I figured out the length and the where clause
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql help 2 45
SubQuery link 4 35
SQLite with Mobile Development 4 12
Finding gaps or missing dates in a date range using TSQL 3 23
CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question