How to get all MSSQL database columns names, data types and length

  • Comments posted to this topic are about the item How to get all MSSQL database columns names, data types and length

  • My way... Since SQL Server 2005 I prefer SYS.Tables before sysobjects with type = 'U'. Although the execution plan looks similar, has some differences. Which returns the best results? I don't know.

    SELECT QUOTENAME(SCHEMA_NAME(tb.[schema_id])) AS 'Schema'

    ,QUOTENAME(OBJECT_NAME(tb.[OBJECT_ID])) AS 'Table'

    ,C.NAME as 'Column'

    ,T.name AS 'Type'

    ,C.max_length

    ,C.is_nullable

    FROM SYS.COLUMNS C INNER JOIN SYS.TABLES tb ON tb.[object_id] = C.[object_id]

    INNER JOIN SYS.TYPES T ON C.system_type_id = T.user_type_id

    WHERE tb.[is_ms_shipped] = 0

    ORDER BY tb.[Name]

  • samehzagloul

    I ran the query against AdventureWorks and the join to data type included column names as a type

    Here is a sample of the output

    TableNameColumnNameNameLength

    AddressAddressIDint4

    AddressAddressLine1nvarchar120

    AddressAddressLine1AccountNumber120

    AddressAddressLine1Name120

    AddressAddressLine1OrderNumber120

    AddressAddressLine1Phone120

    AddressAddressLine2nvarchar120

    AddressAddressLine2AccountNumber120

    AddressAddressLine2Name120

    AddressAddressLine2OrderNumber120

    AddressAddressLine2Phone120

    David Bird

  • No they're not columns, they're types, user defined types.

    In the Object Explorer window in SSMS.

    Open Programmability->Types->User Defined Types

    and you'll see them.

    The problem is that the join in the query is wrong where it joins on the xtype column of systypes it should use xusertype instead.

  • The problem is that the join in the query is wrong where it joins on the xtype column of systypes it should use xusertype instead.

    [font="Courier New"]Did you try the query before you say there was a wrong in the join[/font]

  • samehzagloul (12/9/2010)


    The problem is that the join in the query is wrong where it joins on the xtype column of systypes it should use xusertype instead.

    [font="Courier New"]Did you try the query before you say there was a wrong in the join[/font]

    Yes, I did. Please don't shout.

  • samehzagloul (12/9/2010)


    The problem is that the join in the query is wrong where it joins on the xtype column of systypes it should use xusertype instead.

    [font="Courier New"]Did you try the query before you say there was a wrong in the join[/font]

    Please don't take offence, I was merely pointing out that if there are user defined datatypes in the databse then joining on the xtype column in systypes will return incorrect results (too many rows) as the xtype column is not unique in this case. You need to join on the xusertype column instead which is unique.

    If you look at the systypes table in the AdventureWorks database you will see why there is a problem.

  • Actually I have a few other concerns with these scripts:

    - unicode columns will show up as double length, and numeric columns will be unobvious because the max_length column used is actually a byte length, not character or digit length.

    - numeric columns should use xprec, xscale for syscolumns, or precision, scale for sys.columns because of the same reason, max_length is a byte count

    so I tend to use:

    SELECT table_schema, table_name, column_name, data_type, character_maximum_length,

    is_nullable, column_default, numeric_precision, numeric_scale

    FROM information_schema.columns

    ORDER BY table_schema, table_name, ordinal_position

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply