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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can I use this function? 3 32
SQL Sum of items in two tables not equal. 5 43
SQL- GROUP BY 4 21
the way to learn Microsoft BI 11 37
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
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…

685 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