SqlServer Data table and fields listing

What query can I run that will list all of the tables within a given database and also list all of the column names and their types within each table of that database?
Lenny GrayAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
For starters give this a whirl
SELECT t.name as table_name, c.name as column_name, ty.name as column_type
FROM sys.tables t
   JOIN sys.columns c ON t.object_id = c.object_id
   JOIN sys.types ty ON c.system_type_id = ty.system_type_id
ORDER BY t.name, c.name

Open in new window

Then you do a 'SELECT TOP 10 * FROM sys.types' to see what other data type info you want.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
This will probably give you more than you want, but you could trim it down it you wanted to:

EXEC sp_columns '%', 'dbo'
0
Lenny GrayAuthor Commented:
Jim Horn -

How do I get the varchar/char length to appear on the line too?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
The sys.types have columns max_length, precision, scale that can be added to the query.
SELECT t.name as table_name, c.name as column_name, ty.name as column_type, ty.max_length, ty.[precision], ty.scale
FROM sys.tables t
   JOIN sys.columns c ON t.object_id = c.object_id
   JOIN sys.types ty ON c.system_type_id = ty.system_type_id
ORDER BY t.name, c.name

Open in new window

Scott's sp_columns also has these.
0
Lenny GrayAuthor Commented:
Thanks for the quick response, Jim.

What I need is the actual assignment for a table element.

For instance, EmployeeName, Varchar(50).

EmployeeAge: INT

ActualBid: Decimal(10,6)



Lenny
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Define 'actual assignment'.
0
Lenny GrayAuthor Commented:
Like this
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Explain why you believe the answers we've provided you does not answer the question.
0
Lenny GrayAuthor Commented:
Because this is what I see using your code:

Capture2.PNG
0
Anthony PerkinsCommented:
You are going to find out that this is actually more difficult then you would first think, so you may be better off using Scott's suggestion.  If however you want to pursue it then some of the changes I would recommend is joining against user_type_id instead of system_type_id and using the max_length, length and scale as defined in sys,columns.
0
Anthony PerkinsCommented:
I am sure this is full of errors, but you should get the idea:
SELECT  t.name AS table_name,
	c.column_id,
        c.name AS column_name,
        ty.name AS column_type,
		CASE
			WHEN c.max_length = -1 THEN
				CASE 
					WHEN ty.name IN ('xml') THEN ''
					WHEN ty.name IN ('varbinary', 'varchar', 'nvarchar') THEN '(MAX)'
				END
			WHEN ty.name IN ('text', 'ntext') THEN ''
			WHEN ty.name IN ('nchar', 'nvarchar') THEN '(' + CAST(c.max_length / 2 AS varchar(10)) + ')'
			WHEN ty.name IN ('varbinary', 'char', 'varchar') THEN '(' + CAST(c.max_length AS varchar(10)) + ')'
			WHEN ty.name IN ('numeric', 'decimal') THEN '(' + CAST(c.[precision] AS varchar(10)) + ',' + CAST(c.[scale] AS varchar(10)) + ')'
			-- All the rest go here
			ELSE ''
		END [length]
FROM    sys.tables t
        INNER JOIN sys.columns c ON t.object_id = c.object_id
        INNER JOIN sys.types ty ON c.user_type_id = ty.user_type_id
ORDER BY 
	t.name,
        c.column_id

Open in new window

0
Lenny GrayAuthor Commented:
Thanks Anthony for explaining why my request was difficult.
Thanks Jim for the best solution for an impossible request.
Thanks Scott for a great alternative.

Thanks to all of you for teaching me more about SQL.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the split.  Good luck with your project.  -Jim
0
Lenny GrayAuthor Commented:
Good Morning, All.

Below is my final code. Based upon what you taught me and with some help with cursors, I finally got what I needed. I thought that I would share my results thanks to the hints that the three of you gave to me.

************************ (begin code) ************************

use QRMAssistant

DECLARE @TheTableName VARCHAR(100) -- Table name  

DECLARE db_cursor CURSOR FOR  
SELECT name
FROM [QRMAssistant].[sys].[sysobjects]
Where xtype = 'u'

OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @TheTableName  

WHILE @@FETCH_STATUS = 0  
      BEGIN  
      
      SELECT t.TABLE_NAME as 'Table', c.COLUMN_NAME as 'Column Name' ,c.DATA_TYPE as 'Type' ,c.CHARACTER_MAXIMUM_LENGTH as 'Length',c.NUMERIC_PRECISION as 'Numeric length',c.NUMERIC_SCALE as 'Decimals',c.DATETIME_PRECISION as 'Date/Time'
      FROM INFORMATION_SCHEMA.Tables t
      INNER JOIN INFORMATION_SCHEMA.Columns c ON t.TABLE_CATALOG=c.TABLE_CATALOG AND t.TABLE_SCHEMA=c.TABLE_SCHEMA AND t.TABLE_NAME=c.TABLE_NAME
      WHERE t.TABLE_NAME = @TheTableName
      ORDER BY t.TABLE_NAME, c.COLUMN_NAME


      FETCH NEXT FROM db_cursor INTO @TheTableName  

      END  

CLOSE db_cursor  
DEALLOCATE db_cursor

************************ (end code) ************************

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.