Home Summarize columns in a table using MS SQL Server
Post
Cancel

Summarize columns in a table using MS SQL Server

I realized that it would be handy to have a SQL script for quickly pulling up basic information on the columns of a table in Microsoft SQL server. This is a first approximation that I found online and modified.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- Select the database you want to use.
USE [<DB_NAME>];

-- Set the table you want to summarize.
DECLARE @tabname NVARCHAR(50);
SET @tabname '<TABLE_NAME>';

-- Leave this alone (unless you want to change what is returned)
SELECT
    c.name 'column_name',
    t.Name 'data_type',
    c.max_length 'max_length',
    c.precision,
    c.scale,
    c.is_nullable,
    ISNULL(i.is_primary_key, 0) 'primary_key'
FROM
    sys.columns c
INNER JOIN
    sys.types t ON c.user_type_id = t.user_type_id
LEFT OUTER JOIN
    sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id = c.column_id
WHERE
    c.object_id = OBJECT_ID(@tabname);
This post is licensed under CC BY 4.0 by the author.

Parity Decomposition Plots of Scalar Functions in Python

Vector function checklist