SQL Server将行字符串导入表列

时间:2016-04-04 13:50:37

标签: sql sql-server

我有一个表格,每行包含以下文字

"[0,0,0,1,2,4,1,0,0,2,0,0,0,0,847,18207,0,0,0,0,0,0,0,0,0,0,0,0]"

现在我想在包含28个的表格中插入这28个值。我尝试了一些分割函数,但这些只返回行。

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

使用dbo.fnParseString()

INSERT INTO a_table (col1, col2, col3, . . . )
SELECT dbo.fnParseString(-1, ',', str)
      ,dbo.fnParseString(-2, ',', str)
      ,dbo.fnParseString(-3, ',', str)
      ,....
FROM  yourtable 

答案 1 :(得分:1)

DECLARE @x XML

;with cte as (
SELECT '[0,0,0,1,2,4,1,0,0,2,0,0,0,0,847,18207,0,0,0,0,0,0,0,0,0,0,0,0]' as col
)

SELECT @x= (
SELECT CAST('<s>' + REPLACE(REPLACE(REPLACE(col,'[','<a>'),']','</a>'),',','</a><a>') +'</s>'AS XML)
FROM cte
FOR XML PATH('')
)


SELECT  t.v.value('a[1]','int'),
        t.v.value('a[2]','int'),
        t.v.value('a[3]','int'),
        ...
        t.v.value('a[28]','int')
FROM @x.nodes('/s') as t(v)