首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用列内容创建临时表

如何使用列内容创建临时表
EN

Stack Overflow用户
提问于 2016-09-27 00:18:07
回答 1查看 60关注 0票数 0

我有一个表,它有一个contents字段,其中填充了该页面上项目的ID。如下所示:

代码语言:javascript
运行
复制
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

我想创建一个如下所示的临时表:

代码语言:javascript
运行
复制
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 

这有可能吗?提前感谢您的宝贵时间。

EN

回答 1

Stack Overflow用户

发布于 2016-09-27 01:18:28

来自Turning a Comma Separated string into individual rows

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39707693

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档