Question : All I want is to read the entire schema


1- I want to display all the names of the tables

2- I want to display all the columns names of the tables along with their foreign keys/ relations..

I am using ms sql express 2008 and asp.net
thanks a bundle

Answer : All I want is to read the entire schema

Hi goodk,

below is a new query.

select object_name(col.object_id) as [table],
       col_name(col.object_id, col.column_id) as [column],
       typ.name as [column_type],
       case when ind.is_primary_key = 1 and ind_col.index_id is not null then 'Yes'
             else 'No'
       end as [is_primary_key],
       isnull(object_name(fk.referenced_object_id),'') as [referenced table],
       isnull(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 typ on col.user_type_id = typ.user_type_id
                       left join sys.indexes ind on obj.object_id = ind.object_id
                                                and col.object_id = ind.object_id
                       left join sys.index_columns ind_col on obj.object_id = ind_col.object_id
                                                          and ind.object_id = ind_col.object_id
                                                          and col.column_id = ind_col.column_id
                                                          and ind.index_id = ind_col.index_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' -- listing only user tables
 

Random Solutions  
 
programming4us programming4us