Home > Software engineering >  T-SQL :: List all tables, columns and pivot content
T-SQL :: List all tables, columns and pivot content

Time:11-23

I'm using the SSMS tool enter image description here

This guys apparently arrived to archive that:

  • enter image description here

    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 
    
  • Related