SQL server query to get the list of columns in a table along with Data types, NOT NULL, and PRIMARY KEY constraints
Asked Answered
J

20

301

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
Jordan answered 10/3, 2010 at 16:19 Comment(1)
Could you show the code you have already?Gail
P
584

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.

Percuss answered 10/3, 2010 at 16:37 Comment(29)
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.Roup
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....Percuss
I'm getting an error when I try to run this: Server 'CLM', Line 5 Incorrect syntax near the keyword 'precision'.Osborn
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.Osborn
@NathanJones: as I already said in the answer - this script is for SQL Server 2005 and newer only!Percuss
Sorry about that! I didn't read that last part until after I started having trouble. Thanks for your help!Osborn
You may want to throw a DISTINCT clause in there also... I got some duplicate column names in my results.Trocar
ADDENDUM: Even with DISTINCT you can end up with 2 records for 1 column.Trocar
+1 ahh, but now I want to get the data in my table alongside the column datatypes... any thoughtsReprobation
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 codeDiverting
@Terry: the catalog views return the max length in bytes (not in characters) - already been talked about, see 2nd and 3rd comments ...Percuss
It works on views as well (no surprise) and it also works on Azure SQL.Tronna
Works great for me SQL Server 2012 :)Ediva
WHERE c.object_id = OBJECT_ID('YourTableName') .... I needed WHERE c.object_id = OBJECT_ID('MySchema.MyTableName') and then everything worked fine.Meares
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? ThanksBurly
@molerus: yes - nvarchar (and nchar) are always 2 byte = 1 characterPercuss
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=1Cavicorn
this query has saved lots of time for me, as my tables has 60+ columns. Also this worked perfectly.Pecker
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.Villiers
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: learn.microsoft.com/en-us/sql/t-sql/functions/… which has the same problem apparently.Norwood
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.Norwood
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.Norwood
@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 ......Percuss
It's all good man - you are very active on SO, so thank you for taking the time to respond to me.Norwood
And how to know the foreign Keys in the table with the queryAzelea
@AbdullahAlMamun: check out the sys.foreign_keys catalog viewPercuss
answer should be updated to handle schemas (for instance 2 tables with same name in different schemas)Tempe
@Percuss : 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);Wolfsbane
#2419027Wolfsbane
D
114

The stored procedure sp_columns returns detailed table information.

exec sp_columns MyTable
Donation answered 10/3, 2010 at 17:50 Comment(2)
exec sp_pkeys exec sp_fkeysTuranian
If using this, note that MyTable really is just the table name, not the schema. To filter to a schema, add it as a second parameter: exec sp_columns 'MyTable', 'MySchema'Karlsruhe
A
95

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.

Auxesis answered 10/3, 2010 at 16:44 Comment(2)
i did that :) But i need the PK too :|Jordan
msdn.microsoft.com/pt-br/library/ms189813(v=sql.120).aspx sp_fkeys sp_pkeysTuranian
S
26

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.

Struck answered 30/11, 2016 at 10:23 Comment(1)
This works for Excel files opened with OPENROWSET as well while many other solutions don't. Thank you.Limbus
P
15

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'
Pendulous answered 22/11, 2012 at 9:49 Comment(1)
How does your answer differ from the one posted by Ajadex? Both answers do not return Primary Key information.Savanna
J
15

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')
Jessalin answered 10/3, 2015 at 19:57 Comment(2)
the DDL Name is SO useful for dynamic-sql that creates tables! Thanks!!Pang
This answer with the DDL is great! but I hoped it can be done without hard-coded types names and cases... any way to make it more generic?Euthanasia
N
11

I am a little bit surprised nobody mentioned

sp_help 'mytable'
Necrosis answered 1/5, 2019 at 17:42 Comment(3)
Doesn't work for me.Emilie
@Ac Hybl Did you put the name of your table inside the single quotes? sp_help has worked for me for over 20 years.Necrosis
Thanks for checking in, but yeah. I just tried again and I get a syntax error with single quotes. It probably isn't an issue with your method. It's probably an issue with whatever the backend is for Integrify. I can't access the DB directly. I send queries through my company's workflow management platform.Emilie
Z
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'
Zingale answered 10/3, 2010 at 17:50 Comment(1)
well. It doesnt give me the required result :(Jordan
D
7
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://mcmap.net/q/101754/-select-columns-of-a-view

Depriest answered 19/10, 2017 at 13:38 Comment(2)
if this question has already been answered, flag the post as a duplicate.Arette
using 'information_schema.columns' we can view info for all tables columns including viewing table name and schema.Reamonn
E
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;
Embryo answered 12/3, 2014 at 17:29 Comment(0)
A
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.

Attire answered 22/1, 2015 at 5:55 Comment(1)
He ask for a query, but you're right this way allows you to see all the info.Halting
O
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
Ornamentation answered 22/5, 2013 at 9:33 Comment(0)
W
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'
Wolfsbane answered 14/7, 2020 at 7:20 Comment(0)
H
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.

Hamate answered 14/5, 2018 at 9:8 Comment(1)
your condition of Constraint should be the other way around.Maurice
J
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

Jannjanna answered 31/5, 2019 at 5:27 Comment(0)
F
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.

Fragonard answered 14/1, 2014 at 9:10 Comment(0)
B
1

Throwing another way to tackle the problem in SQL server. My little script here should return the Column Name, Data Type, Is Nullable, Constraints, and Indexes Names. You can also include any additional columns such as precision, scale... (You will need to replace the DB name, Schema Name, and Table Name with yours) .The Columns are returned in the same order you would get from 'select * from table'

USE DBA -- Replace Database Name with yours

DECLARE @SCHEMA VARCHAR(MAX)
DECLARE @TABLE_NAME VARCHAR(MAX)
DECLARE @SCHEMA_TABLE_NAME VARCHAR(MAX)

SET @SCHEMA = REPLACE(REPLACE('[SCHEMA NAME]', '[', ''), ']', '')--Replace Schema Name with yours
SET @TABLE_NAME = REPLACE(REPLACE('[TABLE NAME]', '[', ''), ']', '') --' Replace Table  Name with yours
SET @SCHEMA_TABLE_NAME = @SCHEMA + '.' + @TABLE_NAME;


WITH SchemaColumns
AS (
    SELECT C.COLUMN_NAME,
        IS_NULLABLE,
        DATA_TYPE,
        CHARACTER_MAXIMUM_LENGTH,
        C.ORDINAL_POSITION
    FROM INFORMATION_SCHEMA.COLUMNS AS C
    WHERE C.TABLE_SCHEMA = @SCHEMA
        AND C.TABLE_NAME = @TABLE_NAME
    ),
SchemaConstraints
AS (
    SELECT CN.COLUMN_NAME,
        CC.CONSTRAINT_TYPE
    FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS CC
    INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE AS CN ON CC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME
    WHERE CC.TABLE_SCHEMA = @SCHEMA
        AND CC.TABLE_NAME = @TABLE_NAME
    ),
SchemaIndex
AS (
    SELECT I.name AS index_name,
        COL_NAME(IC.object_id, IC.column_id) AS column_name,
        IC.index_column_id,
        IC.key_ordinal,
        IC.is_included_column
    FROM sys.indexes AS i
    INNER JOIN sys.index_columns AS IC ON I.object_id = IC.object_id
        AND I.index_id = IC.index_id
    WHERE I.object_id = OBJECT_ID(@SCHEMA_TABLE_NAME)
    )
SELECT ISNULL(SchemaColumns.COLUMN_NAME, '') "Column Name",
    CASE 
        WHEN SchemaColumns.CHARACTER_MAXIMUM_LENGTH IS NULL
            THEN UPPER(ISNULL(SchemaColumns.DATA_TYPE, ''))
        ELSE CONCAT (
                UPPER(ISNULL(SchemaColumns.DATA_TYPE, '')),
                '(',
                CAST(SchemaColumns.CHARACTER_MAXIMUM_LENGTH AS VARCHAR(50)),
                ')'
                )
        END "Data Type",
    SchemaColumns.IS_NULLABLE "Is Nullable",
    ISNULL(SchemaConstraints.CONSTRAINT_TYPE, '-') "Constraints",
    ISNULL(STRING_AGG(CONVERT(NVARCHAR(max), SchemaIndex.INDEX_NAME), CHAR(13)), '-') "Indexes Names"
FROM SchemaColumns
LEFT JOIN SchemaConstraints ON SchemaConstraints.COLUMN_NAME = SchemaColumns.COLUMN_NAME
LEFT JOIN SchemaIndex ON SchemaColumns.COLUMN_NAME = SchemaIndex.COLUMN_NAME
GROUP BY SchemaColumns.COLUMN_NAME,
    SchemaColumns.DATA_TYPE,
    SchemaColumns.CHARACTER_MAXIMUM_LENGTH,
    SchemaColumns.IS_NULLABLE,
    SchemaConstraints.CONSTRAINT_TYPE,
    SchemaColumns.ORDINAL_POSITION
ORDER BY SchemaColumns.ORDINAL_POSITION
Bette answered 7/4, 2022 at 16:24 Comment(0)
B
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
Benco answered 28/4, 2016 at 7:40 Comment(2)
Please use indentation instead of inline Markup and add some explaination to your answer.Mycosis
Why ORDER BY 2?Exposure
A
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
Adenoidectomy answered 13/2, 2020 at 13:59 Comment(0)
G
-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')
Girand answered 28/11, 2018 at 10:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.