Search notes:

SQL Server - sys.columns

Joining sys.tables with sys.columns allows to query table and column names.

Showing columns of a table

The following query displays the columns and datatypes of a given table:
select
   sch.name             sch_name,
   tab.name             tab_name,
   col.name             col_name,
-- col.user_type_id,
   ust.name             user_type
-- col.system_type_id,
-- syt.name             system_type
from
   sys.schemas   sch                                               join
   sys.tables    tab on sch.schema_id      = tab.schema_id         join
   sys.columns   col on tab.object_id      = col.object_id         join
   sys.types     ust on col.user_type_id   = ust.user_type_id   -- join
-- sys.types     syt on col.system_type_id = syt.system_type_id
where
   sch.name = 'dbo'                  and
   tab.name = 'foo_bar'
order by
   col.column_id
;
Github repository about-MSSQL, path: /administration/schemas/sys/objects/views/columns/table-columns.sql
Compare with INFORMATION_SCHEMA.columns and sp_columns.

Find tables by column name

A similar query finds all tables that have a column whose name matches a given pattern:
select
   tab.name    table_,
   sch.name    schema_,
   col.name    column_
from
   sys.tables  tab                                    join
   sys.columns col on tab.object_id = col.object_id   join
   sys.schemas sch on tab.schema_id = sch.schema_id
where
   col.name like '%ABC%';
Github repository about-MSSQL, path: /administration/schemas/sys/objects/views/columns/find-tables-by-column-name.sql

See also

See also query objects in a schema to find any object by its name.
sys.syscolumns and sys.all_columns.
The sys schema.

Index