我认为您想使用它来制作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;