在SQL Server中循环从1到60
我有一个表T1如下
我需要将数据从T1复制到另一个称为T2的表。 “月”的额外列,T1的每个记录都需要复制至60次T2,一个月值范围从1到60。
T2还有一个名为 “ rel =“ nofollow noreferrer”>
我一直在尝试这样的事情,需要动态地采取月份的价值,例如1到60的循环。有人可以帮忙吗?谢谢
INSERT INTO T2
SELECT PRODUCT, CUSTOMER, 1 as MONTH
FROM T1
I have a table T1 as below
I need to copy the data from T1 to another table called T2. T2 has an additional column called 'Month' and each record from T1 needs to be copied to T2 60 times, with Month value ranging from 1 to 60.
I have been trying something like this and need the MONTH value to be taken dynamically , like a loop from 1 to 60. Could someone help please? Thank you
INSERT INTO T2
SELECT PRODUCT, CUSTOMER, 1 as MONTH
FROM T1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们可以使用交叉加入方法:
We can use a cross join approach:
交叉连接到一个值1到60的值。-感谢@larnu的答案。
CROSS JOIN to a tally, with the values 1 to 60. -- Thanks @Larnu for the answer.
我在mySQL中使用了存储过程:
I used stored procedures in MySQL: