简体   繁体   中英

Sample SQL Data

I want to present a schema to my BA team.

Select TABLE_NAME, col.DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS col
order by col.TABLE_CATALOG, TABLE_NAME, TABLE_SCHEMA, col.ORDINAL_POSITION

I want to present 3 rows of sample data as well but I wanted to pivot it out so they get an output that has the following 3 columns:

  1. Table_Name
  2. Data_Type
  3. Sample_Data

How do I pivot that data?

Here is a solution based on cursors and dynamic SQL. I've includes the table schema and column names in the final result as well, though the question just asks fro table name, data type, and sample data.

Also, I wasn't sure if you wanted three rows of sample data per table/column, or if you wanted one row per table/column with three columns of sample data. I went with the former, please let me know if you wanted the later. I did include a "No data" indicator for tables that don't have any sample data.

Tested on SQL Server 2005, but I think it should work with 2000 as well.

Create Table #results
    (id           Integer       Not Null Identity(1, 1)
    ,table_schema nVarChar(128) Not Null
    ,table_name   nVarChar(128) Not Null
    ,column_name  nVarChar(128) Not Null
    ,data_type    nVarChar(128) Not Null
    ,sample_data  nVarChar(max)     Null);

Declare @table_name   nVarChar(128)
       ,@table_schema nVarChar(128)
       ,@column_name  nVarChar(128)
       ,@data_type    nVarChar(128)
       ,@sql          nVarChar(max)
       ,@inserted     Integer;

Declare rs Cursor Local Forward_Only Static Read_Only
For Select
         col.table_schema
        ,col.table_name
        ,col.column_name
        ,col.data_type
    From INFORMATION_SCHEMA.COLUMNS col
    Order By col.TABLE_CATALOG
            ,col.TABLE_SCHEMA
            ,col.TABLE_NAME
            ,col.ORDINAL_POSITION
Open rs;

Fetch Next From rs Into @table_schema, @table_name, @column_name, @data_Type;
While @@Fetch_Status = 0 Begin;

    Set @table_schema = QuoteName(@table_schema);
    Set @table_name = QuoteName(@table_name);
    Set @column_name = QuoteName(@column_name);

    Set @sql = N'
    Insert Into #results
        (table_schema
        ,table_name
        ,column_name
        ,data_type
        ,sample_data)
    Select Top 3 ' + QuoteName(@table_schema, '''') + N'
                ,' + QuoteName(@table_name,   '''') + N'
                ,' + QuoteName(@column_name,  '''') + N'
                ,' + QuoteName(@data_type,    '''') + N'
                ,' + @column_name + N'
    From ' + @table_schema + N'.' + @table_name;

    Exec (@sql);

    Select @inserted = count(*)
    From #results
    Where table_schema = @table_schema
    And   table_name   = @table_name
    And   column_name  = @column_name;

    If @inserted = 0
        Insert Into #results (table_schema, table_name, column_name, data_type, sample_data)
        Values (@table_schema, @table_name, @column_name, @data_type, ' -- No Data -- ');

    Fetch Next From rs Into @table_schema, @table_name, @column_name, @data_Type;
End;

Close rs;
Deallocate rs;

-- Probably should include the schema and column name too:
Select table_schema, table_name, column_name, data_type, sample_data
From #results
Order by [id];

-- But this is what the question asked for:
-- Select table_name, data_type, sample_data
-- From #results
-- Order by [id];

Drop Table #results;

There are probably more elegant solutions available, but this should get you started, I think. Good luck!

You can dynamically build queries like this for each table:

SELECT  TOP 3 *
FROM    mytable
FOR XML AUTO

and parse the resulting XML in the presenation layer.

I built this using XML PATH

SET NOCOUNT ON

SELECT 
    'SELECT ''' + col.TABLE_NAME + ''' AS TableName,' +
    '''' + col.COLUMN_NAME + ''' AS ColumnName,'+
    ' ''' +  col.DATA_TYPE + ''' as DataType,   '  +
    '
    (
        SELECT top 3 CONVERT (VARCHAR, p2.' + col.COLUMN_NAME + ') + '','' 
        FROM ' + col.TABLE_SCHEMA + '.' + col.TABLE_NAME + ' p2
        ORDER BY p2. ' +  col.COLUMN_NAME + '
        FOR XML PATH('''') 
    )
    UNION ALL'
FROM INFORMATION_SCHEMA.COLUMNS col
ORDER BY 
    col.TABLE_CATALOG, 
    col.TABLE_NAME, 
    col.TABLE_SCHEMA, 
    col.ORDINAL_POSITION

I copy paste the result of this query into a new query editor window. I delete the last UNION ALL and execute the query.

It gives me an additional comma in the Returned data, but my BA's were OK with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM