日期:2014-05-18 浏览次数:20718 次
CREATE TABLE TT(a_1 VARCHAR(1),a_2 VARCHAR(1),b_1 VARCHAR(1)) GO INSERT INTO tt(a_1,a_2,b_1) SELECT '1','1','b' UNION SELECT '2','2','b' SELECT DISTINCT 'select ' + STUFF(( SELECT ',' + name FROM sys.columns t WHERE object_id = sys.columns.object_id AND name LIKE 'a%' --字段名起始字符 FOR XML PATH('') ), 1, 1, '') + ' from ' + OBJECT_NAME(object_id) FROM sys.columns WHERE object_id = OBJECT_ID('TT')
------解决方案--------------------
可能是我理解能力不好,没懂你的题目的意思!
CREATE TABLE TT(a_1 VARCHAR(1),a_2 VARCHAR(1),b_1 VARCHAR(1)) GO INSERT INTO tt(a_1,a_2,b_1) SELECT '1','1','b' UNION SELECT '2','2','b' SELECT DISTINCT 'select ' + STUFF(( SELECT ',' + name FROM sys.columns t WHERE object_id = sys.columns.object_id AND name LIKE 'a%' FOR XML PATH('') --把TT表中的列名查询出来组成 ,a_1,a_2这种类型字符串 ), 1, 1, '') --第一个开始替换一个字符串为空字符串 + ' from ' + OBJECT_NAME(object_id) FROM sys.columns WHERE object_id = OBJECT_ID('TT')