While 循环创建重复记录

While loop creating duplicate records(While 循环创建重复记录)
本文介绍了While 循环创建重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个非常简单的查询,它需要两个日期(开始和结束日期,例如: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?

代码:

DECLARE @StartDate datetime = '01/01/2018'
    ,@EndDate datetime = '01/31/2018'
DECLARE @StartDateWeek INT  = DATEPART(WK,@StartDate)
    ,@EndDateWeek INT   = DATEPART(WK,@EndDate)
SELECT @StartDateWeek,@EndDateWeek

DECLARE @DateRangeWeekList TABLE(
Date DATE
,WeekNumber INT)

INSERT INTO @DateRangeWeekList
SELECT  Date = @StartDate
        ,WeekNumber = @StartDateWeek
select * from @DateRangeWeekList


WHILE (SELECT MAX(WeekNumber) FROM @DateRangeWeekList) < (SELECT     @EndDateWeek)
BEGIN
INSERT INTO @DateRangeWeekList
SELECT  Date = null
        ,WeekNumber = WeekNumber +1
FROM @DateRangeWeekList
END

select * from @DateRangeWeekList

结果:

Date        WeekNumber
2018-01-01  1
NULL        2
NULL        2
NULL        3
NULL        2
NULL        3
NULL        3
NULL        4
NULL        2 and more (total 18 records)

预期结果

Date       WeekNumber
2018-01-01 1
NULL       2
NULL       3
NULL       4
NULL       5

推荐答案

递归 CTE 在这里比 WHILE 循环要好得多:

A recursive CTE would be far better here than a WHILE loop:

DECLARE @StartDate datetime = '01/01/2018',
        @EndDate datetime = '01/31/2018';

WITH WeekNumbers AS(
    SELECT CONVERT(date,@StartDate) AS [Date],
           1 AS WeekNumber
    UNION ALL
    SELECT NULL,
           WeekNumber + 1
    FROM WeekNumbers
    WHERE WeekNumber <= DATEDIFF(WEEK, @StartDate, @EndDate))
SELECT *
FROM WeekNumbers;

不过,您也可以考虑使用日历表.有很多选择供您选择.

You could also, however, consider using a Calendar table. There are lots of options out there for you.

这篇关于While 循环创建重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Union in SQL while creating XML file(创建 XML 文件时在 SQL 中联合)
strange behavior of SQL Server when sum nodes#39;s values in XML(对 XML 中的节点值求和时 SQL Server 的奇怪行为)
How to update a SQL table column with XML data(如何使用 XML 数据更新 SQL 表列)
How To Save XML Query Results to a File(如何将 XML 查询结果保存到文件)
Extracting XML sub-tags from a clob in Oracle via SQL(通过 SQL 从 Oracle 中的 clob 中提取 XML 子标签)
installed Xampp on Windows 7 32-bit. Errors when starting(在 Windows 7 32 位上安装 Xampp.启动时的错误)