Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
213 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 600 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 1400 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this article, I’ll look at how you can use a backup to start a secondary instance for MongoDB.
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

610 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