SQL Server拆分成表

时间:2013-12-19 01:48:41

标签: sql-server

我有四个带分隔符的字符串'|'

accounts = '1|5|12|18'
debit = '100|50|0|0'
credit = '0|0|20|130'
branches = '121|569|901|1002'

并且必须插入表格列中,如下所示

+----------+-------+--------+----------+
| accounts | debit | credit | branches |
+----------+-------+--------+----------+
|    1     |  100  |   0    |   121    |
|    5     |  50   |   0    |   569    |
|    12    |  0    |   20   |   901    |
|    18    |  0    |   130  |   1002   |
+----------+-------+--------+----------+

请帮助谢谢

1 个答案:

答案 0 :(得分:0)

从该链接看,然后使用dbo.fn_Split,

Declare  @accounts varchar(200) = '1|5|12|18'
Declare @debit varchar(200)= '100|50|0|0'
Declare @credit varchar(200)= '0|0|20|130'
Declare @branches varchar(200)= '121|569|901|1002'
Declare @delim char(1)='|'

insert into table1
select a.value as accounts,b.value as debit,c.value as credit,d.value as branch from dbo.fn_Split(@accounts,@delim,null) a
left join dbo.fn_Split(@debit,@delim,null) b on a.id=b.id
left join dbo.fn_Split(@credit,@delim,null) c on a.id=c.id
left join dbo.fn_Split(@branches,@delim,null) d on a.id=d.id