我想向我的BA团队提供一个架构。
Select TABLE_NAME, col.DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS col
order by col.TABLE_CATALOG, TABLE_NAME, TABLE_SCHEMA, col.ORDINAL_POSITION
我想提供3行示例数据,但我想将其转出,以便获得具有以下3列的输出:
如何转动这些数据?
答案 0 :(得分:1)
这是一个基于游标和动态SQL的解决方案。我在最终结果中也包含了表模式和列名,但问题只是询问了表名,数据类型和样本数据。
另外,我不确定您是否希望每个表/列有三行样本数据,或者您希望每个表/列有一行包含三列样本数据。我和前者一起去,如果你想要的话,请告诉我。我确实为没有任何样本数据的表包含了“无数据”指示符。
在SQL Server 2005上测试过,但我认为它也适用于2000.
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;
可能有更优雅的解决方案,但我认为这应该让你开始。祝你好运!
答案 1 :(得分:0)
您可以为每个表动态构建这样的查询:
SELECT TOP 3 *
FROM mytable
FOR XML AUTO
并在演示层中解析生成的XML
。
答案 2 :(得分:0)
我使用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
我将此查询的结果复制粘贴到新的查询编辑器窗口中。我删除了最后一个UNION ALL并执行查询。
它在返回的数据中给了我一个额外的逗号,但我的B.A.对此没问题。