Pages

Saturday, June 1, 2013

How to list all tables with columns in a database?

Execute the following Microsoft SQL Server T-SQL script to demonstrate the listing of all tables with column information in a database. 
USE AdventureWorks;

SELECT  [Table] = s0.name + '.' + o.name,
        [Column]=c.name,
        [ColumnType] = t.name,             
        [Length] = c.max_length
  FROM   sys.schemas s0
  JOIN   sys.objects o
            ON o.schema_id = s0.schema_id
  JOIN   sys.columns c
            ON o.object_id = c.object_id
  LEFT JOIN (sys.types t
              JOIN  sys.schemas s1
                        ON t.schema_id = s1.schema_id)
                  ON  c.user_type_id = t.user_type_id
  WHERE  o.type = 'U'
  ORDER BY s0.Name, o.Name
  GO

No comments:

Post a Comment