日期:2014-05-17 浏览次数:20643 次
DECLARE @str NVARCHAR(1000)
SET @str = '{["Aasd":12,"Bifg":3,"Csd":1,"Ddd":40],["Aasd":16,"Bifg":5,"Csd":0,"Ddd":49]}'
SET @str = REPLACE(SUBSTRING(@str,3,LEN(@str)-4), '"','')
SELECT
rowid,
fieldname = LEFT(c.NAME, CHARINDEX(':',c.name)-1),
fieldvalue = RIGHT(c.NAME,LEN(c.name)-CHARINDEX(':',c.name))
FROM
(
SELECT rowid = ROW_NUMBER() OVER(ORDER BY GETDATE()), name=CONVERT(XML, '<root><v>'+replace(b.name,',','</v><v>')+'</v></root>') FROM
(SELECT [name]=CONVERT(XML, '<root><v>'+replace(@str,'],[','</v><v>')+'</v></root>')) a
OUTER APPLY
(SELECT [name] = C.v.value('.','NVARCHAR(MAX)') FROM a.[name].nodes('/root/v') C(v)) b
) t
CROSS APPLY
(
SELECT [name] = C.v.value('.','NVARCHAR(MAX)') FROM t.[name].nodes('/root/v') C(v)
) c
/*
rowid fieldname fieldvalue
1 Aasd 12
1 Bifg 3
1 Csd 1
1 Ddd 40
2 Aasd 16
2 Bifg 5
2 Csd 0
2 Ddd 49
*/