I am trying to get a summary of all the tables in my database:
I want the results to look as follows where the last column is a sample of each column in each table in the database:
| schema | table | column | data type | NULL | [Reference_ Data (top ten values)] |
| ------------ | ------------ | ------------ | ------------ | ------------ | ------------ |
| dbo | mytable | mycolumn | int | yes | 1,2,3,4,5,6,7,8,9,10 |
I tried using the following query but it doesn't work
SELECT T.TABLE_SCHEMA AS [Schema], T.TABLE_NAME AS [Table], T.COLUMN_NAME AS [Column], T.DATA_TYPE AS [Data Type], T.IS_NULLABLE AS [NULL], STUFF
(
(
SELECT ',' + T.COLUMN_NAME
FROM T.TABLE_NAME
ORDER BY T.TABLE_NAME FOR XML PATH('')
), 1,2, ''
) [Reference_ Data (top ten values)]
FROM INFORMATION_SCHEMA.COLUMNS AS T
ORDER BY TABLE_SCHEMA, TABLE_NAME
1条答案
按热度按时间pepwfjgg1#
I used a cursor to iterate through each table and column in the
INFORMATION_SCHEMA.COLUMNS
view, which builds and executes a dynamic SQL query for each combination to retrieve the top 10 values from that column Where I stored the results in a temporary table#Top10Values
.This is an example of my fictive database :