Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
samehzagloul
samehzagloul
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
Enrique Martinez
Enrique Martinez
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 395
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]


David Bird
David Bird
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 1186
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
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2822
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

samehzagloul
samehzagloul
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
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
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2822
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

nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2822
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

Chris Harshman
Chris Harshman
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2667 Visits: 3256
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search