我有一个表格,其内容字段填充了该页面上的项目ID。像这样:
ID | Contents
Section-page-id | item-1 item-2 item-3 item-4 item-5
Section-page-id2 | item-6 item-7 item-8 item-9 item-10
我想创建一个如下所示的临时表:
Section-id |ID
item-1 |Section-page-id
item-2 |Section-page-id
item-3 |Section-page-id
item-4 |Section-page-id
item-5 |Section-page-id
item-6 |Section-page-id2
item-7 |Section-page-id2
item-8 |Section-page-id2
item-9 |Section-page-id2
item-10 |Section-page-id2
这甚至可能吗?提前感谢您的时间。
答案 0 :(得分:0)
来自Turning a Comma Separated string into individual rows:
Declare @YourTable table
(
ID varchar(50),
Contents varchar(max)
)
Insert Into @YourTable values
('Section-page-id','item-1 item-2 item-3 item-4 item-5'),
('Section-page-id2','item-6 item-7 item-8 item-9 item-10')
;WITH CTE (ID, SectionId, Contents) as (
select ID, LEFT(Contents, CHARINDEX(' ',Contents+' ')-1),
STUFF(Contents, 1, CHARINDEX(' ',Contents+' '), '')
from @YourTable
union all
select ID, LEFT(Contents, CHARINDEX(' ',Contents+' ')-1),
STUFF(Contents, 1, CHARINDEX(' ',Contents+' '), '')
from CTE
where Contents > ''
)
select SectionId, ID
from CTE
order by ID