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
203 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now