3

是否可以递归地从 information_schema 动态地对表列标题进行字符串化

因此,如果我的数据库中有 2 个表(或者很多),每个表有 5 列

我可以获取查询以按模式查找这些表,然后将所有表列的听者串到另一个表中,结果类似于

table name ¦ string 
Table A    ¦ id,columnName1,columnName2 ,columnName3 ,columnName4 ,columnName5
Table b    ¦ id,columnName1,columnName2 ,columnName3 ,columnName4 ,columnName5

(这是两行顺便说一句)

4

5 回答 5

3

它可以更简单,没有 XML:

declare @columns varchar(1000)
set @columns=''

select @columns = @columns + ',' + name
from sys.columns 
where object_id = object_id('MyTable')
于 2013-10-21T09:09:39.047 回答
2

与andFOR XML PATH('')结合使用的东西可以工作:sys.columnssys.tables

SELECT t.name [TableName], 
(
    SELECT name + ','
    FROM sys.columns c
    WHERE c.object_id = t.object_id
    FOR XML PATH('')
) [String]
FROM sys.tables t

它基本上采用从查询返回的列,格式为 XML,其中元素标签在PATH(''). 但是,如果将其留空,它将省略标签并返回一个字符串。

MSDN 上的文档也有很多不同的示例以及该FOR XML子句的其他用途。

于 2013-04-24T17:11:22.423 回答
1

使用sys.objects比sys.tables更有效。可能这对您有帮助-

SELECT 
      table_name = s.name + '.' + o.name
    , [columns] = STUFF((
        SELECT ', ' + c.name
        FROM sys.columns c WITH (NOWAIT)
        WHERE c.[object_id] = o.[object_id]
        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
FROM (
    SELECT 
          o.[object_id]
        , o.name
        , o.[schema_id]
    FROM sys.objects o WITH (NOWAIT)
    WHERE o.[type] = 'U'
        AND o.is_ms_shipped = 0
) o
INNER JOIN sys.schemas s WITH (NOWAIT) ON o.[schema_id] = s.[schema_id] 
ORDER BY 
      s.name
    , o.name
于 2013-04-25T06:35:03.187 回答
0

这将做到(尽管可能有另一种方式):

;with cte as
(
    Select  TableName = t.TABLE_NAME
            ,ColumnList = Substring((Select ', ' + c2.COLUMN_NAME
                                    From    INFORMATION_SCHEMA.COLUMNS c2
                                    Where   c.TABLE_NAME = c2.TABLE_NAME
                                    For XML Path('')
                                    ), 3, 8000)
            ,ROW_NUMBER() OVER(Partition By t.TABLE_NAME Order By t.TABLE_NAME) RowNumber
    From    INFORMATION_SCHEMA.TABLES t
    join    INFORMATION_SCHEMA.COLUMNS c
        on  t.TABLE_NAME = c.TABLE_NAME
)
Select  TableName
        ,ColumnList
From    cte
Where   RowNumber = 1
于 2013-04-24T17:10:52.420 回答
0

我认为您想使用它来制作DBQuery中所有表的类文件。因此答案

if object_id('tempdb..#t') is not null
    drop table #t

SELECT 
T.name Table_name
,ltrim(rtrim(C.name)) Column_name
,TYPE1.name AS [type_name]
,C.max_length length1
into #t
FROM sys.tables T WITH(NOLOCK)
INNER JOIN sys.columns C WITH(NOLOCK) ON T.OBJECT_ID=C.OBJECT_ID
INNER JOIN sys.types AS TYPE1 with(nolock) ON c.user_type_id=TYPE1.user_type_id
ORDER BY T.name

SELECT 
  #t.Table_name,
  STUFF((
    SELECT ', ' + t2.Column_name+' as ' +t2.type_name + '('+ cast(t2.length1 as varchar) + ')'
    FROM #t t2
    WHERE t2.Table_name = #t.Table_name
    FOR XML PATH (''))
,1,2,'') AS Names
FROM #t
GROUP BY #t.Table_name;

具体回答你Question

if object_id('tempdb..#t') is not null
    drop table #t

SELECT 
T.name Table_name
,ltrim(rtrim(C.name)) Column_name
into #t
FROM sys.tables T WITH(NOLOCK)
INNER JOIN sys.columns C WITH(NOLOCK) ON T.OBJECT_ID=C.OBJECT_ID
where t.name in ('[Table A]','[Table B]')
ORDER BY T.name

SELECT 
  #t.Table_name,
  STUFF((
    SELECT ', ' + t2.Column_name
    FROM #t t2
    WHERE t2.Table_name = #t.Table_name
    FOR XML PATH (''))
,1,2,'') AS Names
FROM #t
GROUP BY #t.Table_name;
于 2013-04-25T06:53:31.163 回答