T-SQL :: 列出所有表、列和数据透视表内容

T-SQL :: List all tables, columns and pivot content

我正在使用 SSMS 工具 Data Discovery and Classification

该工具会自动搜索列名称,例如 %address%%name%%surname%%e-mail%%tax%%zip% 等... 并很好地向您建议可能是什么一个合理的数据。

事实是,在盎格鲁-撒克逊社会之外,列名称不是英语,但可以是法语、西班牙语、意大利语等。

所以我找到了一个可以帮助我根据我的语言列出合理数据的查询:

SELECT schema_name(tab.schema_id) AS schema_name
    ,tab.name AS table_name
    ,col.name AS column_name
    ,t.name AS data_type
    ,NULL as Data_Preview
FROM sys.tables AS tab
INNER JOIN sys.columns AS col ON tab.object_id = col.object_id
LEFT JOIN sys.types AS t ON col.user_type_id = t.user_type_id
ORDER BY schema_name
    ,table_name
    ,column_id;

很好。

但是,如果我可以添加一个名为 Content 的最后一列,即 PIVOT 每一列的内容和每一列的 SELECT TOP 5 并在最后一列中很好地打印出来,那就更好了列。

你能帮我存档吗?

那将是 DYO 数据发现和分类。

编辑:我可能表达不好。

我是 运行 针对 AdventureWorks2019 的 mz 查询:

schema_name table_name column_name data_type Data_Preview
Person Address AddressID int NULL
Person Address AddressLine1 nvarchar NULL
Person Address AddressLine2 nvarchar NULL
Person Address City nvarchar NULL
Person Address StateProvinceID int NULL
Person Address PostalCode nvarchar NULL

我想 PIVOT 每栏的内容(比方说 TOP 5)到最后一栏

schema_name table_name column_name data_type Data_Preview
Person Address AddressID int 1,2,3,4,5
Person Address AddressLine1 nvarchar 1970 Napa Ct.,9833 Mt. Dias Blv.,7484 Roundtree Drive,9539 Glenside Dr,1226 Shoe St.
Person Address AddressLine2 nvarchar NULL,NULL,NULL,NULL,NULL
Person Address City nvarchar Bothell,Bothell,Bothell,Bothell,Bothell
Person Address StateProvinceID int 79,79,79,79,79
Person Address PostalCode nvarchar 98011,98011,98011,98011,98011

每个数据可以用逗号或其他任何方式分隔。结果是我想预览实际在列中的数据:

这些人显然是来存档的:

谢谢

已更新以支持 2016

DROP TABLE IF EXISTS #ColumnsToDisplay

SELECT    ROW_NUMBER () OVER (ORDER BY tab.name) AS Iteration,
          s.name AS SchemaName,
          tab.name AS table_name,
          col.column_id,
          col.name AS column_name,
          t.name AS data_type,
          col.max_length,
          col.precision AS PrecisionNumber,
          CAST(NULL AS VARCHAR(MAX)) AS DataSample
INTO      #ColumnsToDisplay
FROM      sys.tables AS tab
JOIN      sys.schemas AS s
    ON    s.schema_id = tab.schema_id
JOIN      sys.columns AS col
    ON    col.object_id = tab.object_id
LEFT JOIN sys.types AS t
    ON    col.user_type_id = t.user_type_id
     
DECLARE @Iterations       INT = 0,
        @CurrentIteration INT = 1;

SELECT @Iterations = MAX (Iteration)
FROM   #ColumnsToDisplay

WHILE @CurrentIteration <= @Iterations
BEGIN
    DECLARE @CurrentTableName  VARCHAR(100)   = '',
            @CurrentColumnName VARCHAR(100)   = '',
            @DynamicQuery      NVARCHAR(1000) = N''
    DECLARE @Sample VARCHAR(MAX)

    SET @CurrentTableName = '';
    SET @DynamicQuery = N'';
    SELECT @CurrentTableName = CONCAT (ttq.SchemaName, '.', ttq.table_name),
           @CurrentColumnName = ttq.column_name
    FROM   #ColumnsToDisplay AS ttq
    WHERE  ttq.Iteration = @CurrentIteration

    IF (@CurrentTableName = '')
    BEGIN
        SET @CurrentIteration += 1

        CONTINUE
    END

 -- SQL Server 2019
 -- SET @DynamicQuery = CONCAT (N'
 --       SELECT @Sample = STRING_AGG(t.ColumnData,'', '') 
 --       FROM (
 --               SELECT TOP 5  CAST(x.[', @CurrentColumnName, '] AS VARCHAR(MAX)) AS ColumnData 
 --               FROM ', @CurrentTableName, ' AS x 
 --               WHERE x.[', @CurrentColumnName, '] IS NOT NULL
 --       )t')

 -- SQL Server 2016 and lower where Stuff is supported   
    SET @DynamicQuery = CONCAT (N'
    SELECT @Sample =  STUFF((SELECT '', ''+ t.ColumnData  
    FROM (
            SELECT TOP 5 CAST(x.[', @CurrentColumnName, '] AS VARCHAR(MAX)) AS ColumnData 
            FROM ', @CurrentTableName, ' AS x 
            WHERE x.[', @CurrentColumnName, '] IS NOT NULL 
    ) AS t
       FOR XML PATH('''')),1,1,'''')')
        
    EXECUTE sys.sp_executesql @DynamicQuery,
                              N'@Sample VARCHAR(MAX) OUTPUT',
                              @Sample = @Sample OUTPUT

    UPDATE #ColumnsToDisplay
    SET    DataSample = @Sample
    WHERE  Iteration = @CurrentIteration

    SET @CurrentIteration += 1
END

SELECT ctd.Iteration,
       ctd.SchemaName,
       ctd.table_name,
       ctd.column_id,
       ctd.column_name,
       ctd.data_type,
       ctd.max_length,
       ctd.PrecisionNumber,
       ctd.DataSample
FROM   #ColumnsToDisplay AS ctd