sql - 1 记录为多条记录
问题描述
如果我的表为
ID NAME PHNO
1 xyz 7895632147
2 abc 8795412632
3 def 9587412306
我想要输出为
ID NAME PHNO
1 xyz 7895632147
1 xyz 7895632147
1 xyz 7895632147
1 xyz 7895632147
1 xyz 7895632147
2 abc 8795412632
2 abc 8795412632
2 abc 8795412632
2 abc 8795412632
2 abc 8795412632
3 def 9587412306
3 def 9587412306
3 def 9587412306
3 def 9587412306
3 def 9587412306
我需要动态执行它任意次数,这里我举了 5 次作为例子。
如何在 microsoft sql server management studio 中进行操作?
解决方案
使用递归 CTE来执行此操作:
declare @terminator int = 5;
with recursivecte as (
select ID,name,phno ,1 as n from testtable
union all
select ID,name,phno, n +1 from recursivecte
where N < @terminator
)
select * from recursivecte
推荐阅读
- mysql - 在 mysql 数据库上查询后,Sequelize 模型不带 LONGTEXT 类型字段
- node.js - WebStorm 2018.1:我无法从 docker 容器运行远程调试打字稿时遇到断点
- google-apps-script - 使用应用程序脚本插入时单元格公式未更新
- ansible - Ansible win_audit_policy_system 模块不改变值
- python - Python Pandas 动态读取具有不同列大小的多个标题行的 Excel 表
- reactjs - React.js 运行 yarn start 会报错
- networking - 在 netfilter 挂钩中获取网络接口 IPv4 地址
- c - TCP 客户端 - 接收来自未知/无限大小的消息
- r - 高效的 R 光栅代数
- python - Python运行线程时丢失数据