views:

125

answers:

3
+1  Q: 

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?

A: 

You can dynamically build quieries like this for each table:

SELECT  TOP 3 *
FROM    mytable
FOR XML AUTO

and parse the resulting XML in the presenation layer.

Quassnoi
@Quassnoi: I want to present it all in a T-SQL query and then copy paste into Excel.
Raj More
I have seen previously an T-SQL XML code snippet to do exactly this, but I do not seem to be able to find it now.
Raj More
+1  A: 

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!

Chris Nielsen
A: 

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 B.A.'s were OK with that.

Raj More