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
208 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 41

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
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…

734 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