--    query: Provides a query of the top 10 rows, for the given table.  The column in focus is put at the beginning of the query.  
--    schema_name:  The schema_name provides the name of the schema of the table, it is often a dbo.
--    table_name: This indicates name of the given table. 
--    column_data:  This provides an xml representation of the table columns. 
--         id: indicates the column id for a given table.
--         name: provides the name of the column name.
--         type: provides the data type of the column
--         length: provides the capacity of the column
--         nullable: indicates if the column is nullable
--         precision: provides the precision of number types
--    column_count:  This provides the total number of columns for the given table.



With Table_CTE As (
    SELECT TABLE_NAME = name, table_id = object_id, SCHEMA_NAME = SCHEMA_NAME(schema_id)
    FROM sys.tables
),
ColumnGroup_CTE As (
    SELECT table_id = object_id, column_count = COUNT (column_id) 
    FROM sys.columns columns
    GROUP BY object_id
),
Query_CTE As (
    SELECT 
        query = 'select top 10 * from [' + SCHEMA_NAME + '].[' + TABLE_NAME + ']' 
        , schema_name
        , table_name
        , column_data =
         (
            SELECT 
            (
                SELECT 
                   '@id' = column_id,
                   '@name' = name,
                   '@type' = Type_Name( system_type_id ),
                   '@length' = max_length ,
                   '@nullable' = CASE WHEN is_nullable = 0 then null ELSE 'true' END,
                   '@precision' = CASE WHEN PRECISION = 0 then null else PRECISION END
                  FROM sys.columns columns
                  WHERE Table_CTE.table_id = object_id
                  order by column_id
                  For Xml Path('column'), type
            )
            For Xml Path('table'), type
        )
        , column_count
   FROM Table_CTE
   LEFT JOIN ColumnGroup_CTE 
        ON ColumnGroup_CTE.table_id = Table_CTE.table_id
)
SELECT *
FROM Query_CTE
ORDER BY TABLE_NAME