首页 > 解决方案 > 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 中进行操作?

标签: sql

解决方案


使用递归 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

推荐阅读