问题描述
我有一个非常简单的查询,它需要两个日期(开始和结束日期,例如:01/01/2018-01/31/2018),然后取出开始和结束日期的周数(例如:1 和5).我使用 while 循环在一张表中输入所有周数(例如:1、2、3、4、5).我的 while 循环正在创建重复记录,例如所有值的多个记录.我不确定是什么问题.有人可以建议我可能做错了什么吗?
I have a very simple query that takes two dates (start and end date, ex: 01/01/2018-01/31/2018) and then take out the week number of start and end date (ex: 1 and 5). I use while loop in order to enter all week numbers in one table (ex: 1,2,3,4,5). My while loop is creating duplicate records, like multiple records for all values. I am not sure what the issue is. Can someone suggest what I am might be doing wrong?
代码:
结果:
预期结果
推荐答案
递归 CTE 在这里比 WHILE
循环要好得多:
A recursive CTE would be far better here than a WHILE
loop:
不过,您也可以考虑使用日历表.有很多选择供您选择.
You could also, however, consider using a Calendar table. There are lots of options out there for you.
这篇关于While 循环创建重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!