251

I need to write a query on SQL server to get the list of columns in a particular table, its associated data types (with length) and if they are not null. And I have managed to do this much.

But now i also need to get, in the same table, against a column - TRUE if that column is a primary key.

How do i do this?

My expected output is:

Column name | Data type | Length | isnull | Pk
Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
Shrayas
  • 6,040
  • 11
  • 34
  • 54

19 Answers19

509

To avoid duplicate rows for some columns, use user_type_id instead of system_type_id.

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
LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c.object_id = OBJECT_ID('YourTableName')

Just replace YourTableName with your actual table name - works for SQL Server 2005 and up.

In case you are using schemas, replace YourTableName by YourSchemaName.YourTableName where YourSchemaName is the actual schema name and YourTableName is the actual table name.

Sylvain Gantois
  • 632
  • 8
  • 26
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
  • 2
    This gives wrong lengths for the nvarchar, etc type of columns. It gives the byte length that is twice as much as the length in the column type. – Andrew Savinykh May 17 '12 at 21:04
  • 15
    Those lengths are **not wrong** - it does give the byte length - that's the maximum possible length in bytes... if you want to calculate space etc., that's the length you want to get.... – marc_s May 18 '12 at 05:20
  • I'm getting an error when I try to run this: Server 'CLM', Line 5 Incorrect syntax near the keyword 'precision'. – Nathan Jones Oct 19 '12 at 17:11
  • 1
    I'm running it in SQL Server 2000. I changed all of the field names to their SQL Server 2000 counterparts, and it works now. – Nathan Jones Oct 19 '12 at 18:21
  • @NathanJones: as I already said in the answer - this script is for SQL Server **2005 and newer** only! – marc_s Oct 19 '12 at 18:22
  • Sorry about that! I didn't read that last part until after I started having trouble. Thanks for your help! – Nathan Jones Oct 19 '12 at 18:36
  • You may want to throw a DISTINCT clause in there also... I got some duplicate column names in my results. – Ed Pecyna Feb 28 '13 at 18:58
  • ADDENDUM: Even with DISTINCT you can end up with 2 records for 1 column. – Ed Pecyna Feb 28 '13 at 19:44
  • +1 ahh, but now I want to get the data in my table alongside the column datatypes... any thoughts – Our Man in Bananas Mar 22 '13 at 11:12
  • Worked well for me in Django using pyodbc and FreeTDS on fedora when I needed to augment the simple `manage.py inspectdb` command. Here's the [code](http://pastebin.com/TJUYyQ0j) – hobs Dec 09 '13 at 22:56
  • @Terry: the catalog views return the max length **in bytes** (not in characters) - already been talked about, see 2nd and 3rd comments ... – marc_s Mar 10 '15 at 19:56
  • It works on views as well (no surprise) and it also works on Azure SQL. – Santhos Mar 12 '15 at 16:12
  • 2
    Works great for me SQL Server 2012 :) – Doc Holiday Aug 21 '15 at 13:05
  • 2
    WHERE c.object_id = OBJECT_ID('YourTableName') .... I needed WHERE c.object_id = OBJECT_ID('MySchema.MyTableName') and then everything worked fine. – Ivan Jun 20 '16 at 11:27
  • Ok, with that awesome query i get the byte length for every column. I need to know how many characters fit in nvarchar columns, can i simply divide maxlength between 2 to find the answer? I have a nvarchar(10) column, maxlength returns 20, can i consider that a single character always needs 2 bytes to be stored in ddbb? What does the byte per character number depend on? Thanks – molerus Dec 29 '16 at 07:32
  • @molerus: yes - `nvarchar` (and `nchar`) are always 2 byte = 1 character – marc_s Dec 29 '16 at 07:56
  • 10
    This query returns duplicated columns if you have multiple indexes involving the same column. To fix it, replace the last two joins with the following: `LEFT OUTER JOIN sys.index_columns ic LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id ON ic.object_id = c.object_id AND ic.column_id = c.column_id AND i.is_primary_key=1` – Razvan Socol Feb 05 '17 at 07:07
  • 2
    this query has saved lots of time for me, as my tables has 60+ columns. Also this worked perfectly. – Akshay Chawla May 24 '17 at 08:13
  • I found that I still received some duplicates with the left outer joins. Once I removed them and the Primary Key column, the duplicates disappeared. Wrapping this within a function would allow you to insert these rows into the return table and then update if the primary key is still needed. The table returns quickly. – yeOldeDataSmythe Aug 27 '18 at 22:56
  • I think it is a bit deceptive to say that max_length is your length when it absolutely isn't. Pretty much every query I have found makes this false assumption. I don't understand why there isn't a separate column for just giving me the length. Instead this is what I found that I have to use: https://docs.microsoft.com/en-us/sql/t-sql/functions/col-length-transact-sql?view=sql-server-2017 which has the same problem apparently. – dyslexicanaboko Sep 13 '18 at 20:22
  • 1
    You can say that's obvious but it isn't. It should be explicitly stated and already available to the user. Why should I have to do this obvious calculation? Just give me the same length I see in my editor and when I define my schema. – dyslexicanaboko Sep 13 '18 at 20:26
  • You are making my point for me - IT DOES DO THAT. I am looking at my editor and I see 255, I query the column and it is returning 510. I don't want to see 510 I want to see 255 which is what I inputted. I don't want to be tasked with dividing it by 2 which is pretty annoying. That's not what I entered. – dyslexicanaboko Sep 13 '18 at 20:33
  • 1
    @dyslexicanaboko: you're right - and I'm baffled - I was convinced it showed the length in characters - not bytes. Probably never realized since I always use `VARCHAR` and hardly ever Unicode ...... – marc_s Sep 13 '18 at 20:38
  • It's all good man - you are very active on SO, so thank you for taking the time to respond to me. – dyslexicanaboko Sep 16 '18 at 01:59
  • And how to know the foreign Keys in the table with the query – Abdullah Al Mamun Jan 12 '19 at 05:15
  • @AbdullahAlMamun: check out the `sys.foreign_keys` catalog view – marc_s Jan 12 '19 at 07:04
  • answer should be updated to handle schemas (for instance 2 tables with same name in different schemas) – Sylvain Gantois Jun 27 '19 at 04:34
  • @marc_s : unfortuntaely, this has a flaw if the Primary key column(s) appear in other indexes: create table dbo.DummyTable ( id int not null identity(0,1) primary key, Msg varchar(80) null ); create index NC_DummyTable_id ON DummyTable(id); – Mitch Wheat Jul 14 '20 at 06:07
  • https://stackoverflow.com/questions/2418527/sql-server-query-to-get-the-list-of-columns-in-a-table-along-with-data-types-no/62889956#62889956 – Mitch Wheat Jul 16 '20 at 03:23
100

The stored procedure sp_columns returns detailed table information.

exec sp_columns MyTable
decompiled
  • 1,705
  • 3
  • 13
  • 19
81

You could use the query:

select COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, 
       NUMERIC_PRECISION, DATETIME_PRECISION, 
       IS_NULLABLE 
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='TableName'

to get all the metadata you require except for the Pk information.

Léa Massiot
  • 1,694
  • 6
  • 21
  • 37
Ajadex
  • 2,119
  • 1
  • 19
  • 22
21

In SQL 2012 you can use:

EXEC sp_describe_first_result_set N'SELECT * FROM [TableName]'

This will give you the column names along with their properties.

rghome
  • 7,212
  • 8
  • 34
  • 53
Amruta Kar
  • 311
  • 2
  • 2
  • This works for Excel files opened with OPENROWSET as well while many other solutions don't. Thank you. – Uttam Sep 17 '20 at 19:21
14

Try this:

select COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, IS_NULLABLE 
from INFORMATION_SCHEMA.COLUMNS IC
where TABLE_NAME = 'tablename' and COLUMN_NAME = 'columnname'
bytecode77
  • 12,331
  • 27
  • 101
  • 126
khaleel
  • 157
  • 1
  • 2
  • 2
    How does your answer differ from the one posted by Ajadex? Both answers do not return Primary Key information. – Artemix Nov 23 '12 at 07:35
10

To ensure you obtain the right length you would need to consider unicode types as a special case. See code below.

For further information see: https://msdn.microsoft.com/en-us/library/ms176106.aspx

SELECT 
   c.name 'Column Name',
   t.name,
   t.name +
   CASE WHEN t.name IN ('char', 'varchar','nchar','nvarchar') THEN '('+

             CASE WHEN c.max_length=-1 THEN 'MAX'

                  ELSE CONVERT(VARCHAR(4),

                               CASE WHEN t.name IN ('nchar','nvarchar')

                               THEN  c.max_length/2 ELSE c.max_length END )

                  END +')'

          WHEN t.name IN ('decimal','numeric')

                  THEN '('+ CONVERT(VARCHAR(4),c.precision)+','

                          + CONVERT(VARCHAR(4),c.Scale)+')'

                  ELSE '' END

   as "DDL name",
   c.max_length 'Max Length in Bytes',
   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
LEFT OUTER JOIN 
   sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
   c.object_id = OBJECT_ID('YourTableName')
Microsoft Developer
  • 1,731
  • 1
  • 18
  • 27
7

Expanding on Alex's answer, you can do this to get the PK constraint

Select C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH, C.NUMERIC_PRECISION, C.IS_NULLABLE, TC.CONSTRAINT_NAME
From INFORMATION_SCHEMA.COLUMNS As C
    Left Join INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
      On TC.TABLE_SCHEMA = C.TABLE_SCHEMA
          And TC.TABLE_NAME = C.TABLE_NAME
          And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
Where C.TABLE_NAME = 'Table'

I must have missed that you want a flag to determine if the given column was part of the PK instead of the name of the PK constraint. For that you would use:

Select C.COLUMN_NAME, C.DATA_TYPE, C.CHARACTER_MAXIMUM_LENGTH
    , C.NUMERIC_PRECISION, C.NUMERIC_SCALE
    , C.IS_NULLABLE
    , Case When Z.CONSTRAINT_NAME Is Null Then 0 Else 1 End As IsPartOfPrimaryKey
From INFORMATION_SCHEMA.COLUMNS As C
    Outer Apply (
                Select CCU.CONSTRAINT_NAME
                From INFORMATION_SCHEMA.TABLE_CONSTRAINTS As TC
                    Join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE As CCU
                        On CCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
                Where TC.TABLE_SCHEMA = C.TABLE_SCHEMA
                    And TC.TABLE_NAME = C.TABLE_NAME
                    And TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                    And CCU.COLUMN_NAME = C.COLUMN_NAME
                ) As Z
Where C.TABLE_NAME = 'Table'
Thomas
  • 61,164
  • 11
  • 91
  • 136
6

Throwing another answer into the ring, this will give you those columns and more:

SELECT col.TABLE_CATALOG AS [Database]
     , col.TABLE_SCHEMA AS Owner
     , col.TABLE_NAME AS TableName
     , col.COLUMN_NAME AS ColumnName
     , col.ORDINAL_POSITION AS OrdinalPosition
     , col.COLUMN_DEFAULT AS DefaultSetting
     , col.DATA_TYPE AS DataType
     , col.CHARACTER_MAXIMUM_LENGTH AS MaxLength
     , col.DATETIME_PRECISION AS DatePrecision
     , CAST(CASE col.IS_NULLABLE
                WHEN 'NO' THEN 0
                ELSE 1
            END AS bit)AS IsNullable
     , COLUMNPROPERTY(OBJECT_ID('[' + col.TABLE_SCHEMA + '].[' + col.TABLE_NAME + ']'), col.COLUMN_NAME, 'IsIdentity')AS IsIdentity
     , COLUMNPROPERTY(OBJECT_ID('[' + col.TABLE_SCHEMA + '].[' + col.TABLE_NAME + ']'), col.COLUMN_NAME, 'IsComputed')AS IsComputed
     , CAST(ISNULL(pk.is_primary_key, 0)AS bit)AS IsPrimaryKey
  FROM INFORMATION_SCHEMA.COLUMNS AS col
       LEFT JOIN(SELECT SCHEMA_NAME(o.schema_id)AS TABLE_SCHEMA
                      , o.name AS TABLE_NAME
                      , c.name AS COLUMN_NAME
                      , i.is_primary_key
                   FROM sys.indexes AS i JOIN sys.index_columns AS ic ON i.object_id = ic.object_id
                                                                     AND i.index_id = ic.index_id
                                         JOIN sys.objects AS o ON i.object_id = o.object_id
                                         LEFT JOIN sys.columns AS c ON ic.object_id = c.object_id
                                                                   AND c.column_id = ic.column_id
                  WHERE i.is_primary_key = 1)AS pk ON col.TABLE_NAME = pk.TABLE_NAME
                                                  AND col.TABLE_SCHEMA = pk.TABLE_SCHEMA
                                                  AND col.COLUMN_NAME = pk.COLUMN_NAME
 WHERE col.TABLE_NAME = 'YourTableName'
   AND col.TABLE_SCHEMA = 'dbo'
 ORDER BY col.TABLE_NAME, col.ORDINAL_POSITION;
JustinStolle
  • 3,732
  • 2
  • 32
  • 46
6

I am a little bit surprised nobody mentioned

sp_help 'mytable'
Mario Levesque
  • 846
  • 10
  • 9
5

wite the table name in the query editor select the name and press Alt+F1 and it will bring all the information of the table.

Abu Zafor
  • 270
  • 2
  • 8
5
SELECT COLUMN_NAME, IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM information_schema.columns WHERE table_name = '<name_of_table_or_view>'

Run SELECT * in the above statement to see what information_schema.columns returns.

This question has been previously answered - https://stackoverflow.com/a/11268456/6169225

Marquistador
  • 1,421
  • 16
  • 21
3
IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES 
     WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = 'Table')
      BEGIN
        SELECT COLS.COLUMN_NAME, COLS.DATA_TYPE, COLS.CHARACTER_MAXIMUM_LENGTH, 
              (SELECT 'Yes' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
                              ON COLS.TABLE_NAME = TC.TABLE_NAME 
                             AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
                             AND KCU.TABLE_NAME = TC.TABLE_NAME
                             AND KCU.CONSTRAINT_NAME = TC.CONSTRAINT_NAME
                             AND KCU.COLUMN_NAME = COLS.COLUMN_NAME) AS KeyX
        FROM INFORMATION_SCHEMA.COLUMNS COLS WHERE TABLE_NAME = 'Table' ORDER BY KeyX DESC, COLUMN_NAME
      END
nasaa
  • 2,421
  • 7
  • 42
  • 71
Pete vM
  • 31
  • 1
3

marc_s's answer is good but it has a flaw if the primary key column(s) appear in other indexes in that those columns will appear more than once. e.g.

Demo:

create table dbo.DummyTable
(
    id int not null identity(0,1) primary key,
    Msg varchar(80) null
);

create index NC_DummyTable_id ON DummyTable(id);

Here's my stored procedure to solve problem:

create or alter procedure dbo.GetTableColumns
(
    @schemaname nvarchar(128),
    @tablename nvarchar(128)
)
AS
BEGIN
    SET NOCOUNT ON;

    with ctePKCols as
    (
        select 
            i.object_id,
            ic.column_id
        from 
            sys.indexes i
            join sys.index_columns ic ON i.object_id = ic.object_id AND i.index_id = ic.index_id
        where 
            i.is_primary_key = 1
    )
    SELECT
        c.name AS column_name,
        t.name AS typename,
        c.max_length AS MaxLength,
        c.precision,
        c.scale,
        c.is_nullable,
        is_primary_key = CASE WHEN ct.column_id IS NOT NULL THEN 1 ELSE 0 END
    FROM 
        sys.columns c
        JOIN sys.types t ON t.user_type_id = c.user_type_id
        LEFT JOIN ctePKCols ct ON ct.column_id = c.column_id AND ct.object_id = c.object_id
    WHERE 
        c.object_ID = OBJECT_ID(quotename(@schemaname) + '.' + quotename(@tablename))
    
END 
GO

exec dbo.GetTableColumns 'dbo', 'DummyTable'
Mitch Wheat
  • 280,588
  • 41
  • 444
  • 526
2

Find combine result for Datatype and Length and is nullable in form of "NULL" and "Not null" Use below query.

SELECT c.name AS 'Column Name',
       t.name + '(' + cast(c.max_length as varchar(50)) + ')' As 'DataType',
       case 
         WHEN  c.is_nullable = 0 then 'null' else 'not null'
         END AS 'Constraint'
  FROM sys.columns c
  JOIN sys.types t
    ON c.user_type_id = t.user_type_id
 WHERE c.object_id    = Object_id('TableName')

you will find result as shown below.

enter image description here

Thank you.

Ankit Mori
  • 547
  • 7
  • 17
2

enter image description here

Query : EXEC SP_DESCRIBE_FIRST_RESULT_SET N'SELECT ANNUAL_INCOME FROM [DB_NAME].[DBO].[EMPLOYEE]'

NOTE: IN SOME IDE BEFORE SELECT N IS WORKING OR, IN SOME IDE WITHOUT N IS WORKING

Abdullah Pariyani
  • 1,332
  • 1
  • 7
  • 25
1
select
      c.name as [column name], 
      t.name as [type name],
      tbl.name as [table name]
from sys.columns c
         inner join sys.types t 
      on c.system_type_id = t.system_type_id 
         inner join sys.tables tbl
      on c.object_id = tbl.object_id
where
      c.object_id = OBJECT_ID('YourTableName1') 
          and 
      t.name like '%YourSearchDataType%'
union
(select
      c.name as [column name], 
      t.name as [type name],
      tbl.name as [table name]
from sys.columns c
         inner join sys.types t 
      on c.system_type_id = t.system_type_id 
         inner join sys.tables tbl
      on c.object_id = tbl.object_id
where
      c.object_id = OBJECT_ID('YourTableName2') 
          and 
      t.name like '%YourSearchDataType%')
union
(select
      c.name as [column name], 
      t.name as [type name],
      tbl.name as [table name]
from sys.columns c
         inner join sys.types t 
      on c.system_type_id = t.system_type_id 
         inner join sys.tables tbl
      on c.object_id = tbl.object_id
where
      c.object_id = OBJECT_ID('YourTableName3') 
          and 
      t.name like '%YourSearchDataType%')
order by tbl.name

To search which column is in which table based on your search data type for three different table in one database. This query is expandable to 'n' tables.

mtinyavuz
  • 35
  • 5
0
SELECT  
   T.NAME AS [TABLE NAME]
   ,C.NAME AS [COLUMN NAME]
   ,P.NAME AS [DATA TYPE]
   ,P.MAX_LENGTH AS [Max_SIZE]
   ,C.[max_length] AS [ActualSizeUsed]
   ,CAST(P.PRECISION AS VARCHAR) +'/'+ CAST(P.SCALE AS VARCHAR) AS [PRECISION/SCALE]
FROM SYS.OBJECTS AS T
JOIN SYS.COLUMNS AS C
    ON T.OBJECT_ID = C.OBJECT_ID
JOIN SYS.TYPES AS P
    ON C.SYSTEM_TYPE_ID = P.SYSTEM_TYPE_ID
    AND C.[user_type_id] = P.[user_type_id]
WHERE T.TYPE_DESC='USER_TABLE'
  AND T.name = 'InventoryStatus'
ORDER BY 2
Toxantron
  • 2,098
  • 8
  • 22
Rajiv Singh
  • 654
  • 7
  • 13
0

There is no primary key here, but this can help other users who would just like to have a table name with field name and basic field properties

USE [**YourDB**]
GO
SELECT tbl.name, fld.[Column Name],fld.[Constraint],fld.DataType 
FROM sys.all_objects as tbl left join 
(SELECT c.OBJECT_ID,  c.name AS 'Column Name',
       t.name + '(' + cast(c.max_length as varchar(50)) + ')' As 'DataType',
       case 
         WHEN  c.is_nullable = 0 then 'null' else 'not null'
         END AS 'Constraint'
  FROM sys.columns c
  JOIN sys.types t
    ON c.user_type_id = t.user_type_id
) as fld on tbl.OBJECT_ID = fld.OBJECT_ID
WHERE ( tbl.[type]='U' and tbl.[is_ms_shipped] = 0)
ORDER BY tbl.[name],fld.[Column Name]
GO
-1

I just made marc_s "presentation ready":

SELECT 
    c.name 'Column Name',
    t.name 'Data type',
    IIF(t.name = 'nvarchar', c.max_length / 2, c.max_length) 'Max Length',
    c.precision 'Precision',
    c.scale 'Scale',
    IIF(c.is_nullable = 0, 'No', 'Yes') 'Nullable',
    IIF(ISNULL(i.is_primary_key, 0) = 0, 'No', 'Yes') '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
LEFT OUTER JOIN 
    sys.indexes i ON ic.object_id = i.object_id AND ic.index_id = i.index_id
WHERE
    c.object_id = OBJECT_ID('YourTableName')
krs
  • 455
  • 2
  • 15