sqlserver查询库中所有表的字段并进行拼接

-- 查询库中所有表的字段信息
SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbo'
ORDER BY TABLE_NAME, ORDINAL_POSITION;

-- 查询库中所有表的字段并进行拼接
SELECT TABLE_NAME, 
       STUFF((SELECT ', ' + COLUMN_NAME 
              FROM INFORMATION_SCHEMA.COLUMNS 
              WHERE TABLE_SCHEMA = 'dbo' 
                AND TABLE_NAME = t.TABLE_NAME 
              FOR XML PATH('')), 1, 2, '') AS COLUMNS
FROM INFORMATION_SCHEMA.COLUMNS AS t
WHERE TABLE_SCHEMA = 'dbo'
GROUP BY TABLE_NAME;
posted @ 2023-11-05 15:18  panbin_2006  阅读(82)  评论(0编辑  收藏  举报