Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to get all MSSQL database columns names, data types and length Expand / Collapse
Author
Message
Posted Wednesday, December 08, 2010 9:53 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 09, 2013 8:38 AM
Points: 1, Visits: 10
Comments posted to this topic are about the item How to get all MSSQL database columns names, data types and length
Post #1032279
Posted Thursday, December 09, 2010 2:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, December 02, 2013 6:51 PM
Points: 4, Visits: 326
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]

Post #1032340
Posted Thursday, December 09, 2010 7:04 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, March 26, 2014 7:24 AM
Points: 182, Visits: 996
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
TableName	ColumnName	Name	Length
Address AddressID int 4
Address AddressLine1 nvarchar 120
Address AddressLine1 AccountNumber 120
Address AddressLine1 Name 120
Address AddressLine1 OrderNumber 120
Address AddressLine1 Phone 120
Address AddressLine2 nvarchar 120
Address AddressLine2 AccountNumber 120
Address AddressLine2 Name 120
Address AddressLine2 OrderNumber 120
Address AddressLine2 Phone 120



David Bird

My PC Quick Reference Guide
Post #1032442
Posted Thursday, December 09, 2010 8:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 7:47 AM
Points: 1,174, Visits: 2,635
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.


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #1032491
Posted Thursday, December 09, 2010 11:18 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, April 09, 2013 8:38 AM
Points: 1, Visits: 10

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.


Did you try the query before you say there was a wrong in the join
Post #1032615
Posted Friday, December 10, 2010 2:24 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 7:47 AM
Points: 1,174, Visits: 2,635
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.


Did you try the query before you say there was a wrong in the join


Yes, I did. Please don't shout.


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #1032886
Posted Friday, December 10, 2010 2:29 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 7:47 AM
Points: 1,174, Visits: 2,635
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.


Did you try the query before you say there was a wrong in the join


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.


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #1032887
Posted Monday, December 13, 2010 1:53 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 10:12 AM
Points: 1,786, Visits: 1,924
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

Post #1034068
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse