Sql 从存储过程中的双分隔字符串创建表

Sql 从存储过程中的双分隔字符串创建表,sql,tsql,stored-procedures,Sql,Tsql,Stored Procedures,为存储过程指定一个输入值,例如: 264#20,241#15,228#10 我如何将其转换为如下表 ID1 ID2 264 20 241 15 228 10 谢谢您可以在存储过程中使用类似的内容: ;with cte (item, col1) as ( select cast(left(col1, charindex(',',col1+',')-1) as varchar(50)) item, stuff(col1, 1, ch

为存储过程指定一个输入值,例如:

264#20,241#15,228#10
我如何将其转换为如下表

ID1     ID2
264     20
241     15
228     10

谢谢

您可以在存储过程中使用类似的内容:

;with cte (item, col1) as
(
  select 
    cast(left(col1, charindex(',',col1+',')-1) as varchar(50)) item,
         stuff(col1, 1, charindex(',',col1+','), '') col1
  from yourtable
  union all
  select 
    cast(left(col1, charindex(',',col1+',')-1) as varchar(50)) item,
    stuff(col1, 1, charindex(',',col1+','), '') col1
  from cte
  where col1 > ''
),
s2 (id1, id2) as
(
  select substring(item, 1, charindex('#', item)-1), 
    reverse(substring(reverse(item), 1, charindex('#', reverse(item))-1))
  from cte
)
select id1, id2
from s2

结果:

| ID1 | ID2 |
-------------
| 264 |  20 |
| 241 |  15 |
| 228 |  10 |
这是一个给你的另一个想法。