一个数据库问题
现在数据中有一个字段 a,b,c,d
我要变成
列名
a
b
c
d
[解决办法]
DECLARE @s VARCHAR(1000)SET @s = 'a,b,c,d'--1.DECLARE @s1 VARCHAR(1000)SET @s1 = RIGHT(REPLACE(',' + @s, ',', ''' AS s UNION SELECT '''), LEN(REPLACE(',' + @s, ',', ''' AS s UNION SELECT ''')) - 12) + ''''EXEC(@s1)--2SELECT REPLACE(REVERSE(( LEFT(s, CHARINDEX(',', s)) )), ',', '') AS SFROM ( SELECT r , REVERSE(LEFT(@s, r)) + ',' AS s FROM ( SELECT ( SELECT COUNT (*) FROM sysobjects WHERE name<= t.name ) AS r FROM sysobjects t ) a WHERE r <= LEN(@s) AND LEFT(@s + ',', r + 1) LIKE '%,' ) tORDER BY r
[解决办法]
我发现嫂子写代码巨快而且巨霸气的
[解决办法]
IF EXISTS (SELECT 1 FROM SYSOBJECTS WHERE name = 'tba')BEGIN DROP TABLE tbaENDGOCREATE TABLE tba( ID INT, Memo VARCHAR(100))GOINSERT INTO tbaSELECT 1,'a,b,c,d' UNIONSELECT 2,'e,f,g,h,k'GODECLARE @sql VARCHAR(1000) = ''SELECT @sql = @sql + 'SELECT ''' + REPLACE(Memo, ',', ''' AS Col UNION SELECT ''') + ''' UNION 'FROM tbaSET @sql = LEFT(@sql,LEN(@sql) - 5)EXEC (@sql)Colabcdefghk
[解决办法]