简体   繁体   English

如何在 Sql Server 中自动递增日期?

[英]How to auto-increment dates in Sql Server?

I have two SQL Server tables, tb_exam and Auxiliary_Calculator .我有两个 SQL Server 表, tb_examAuxiliary_Calculator

tb_exam

Date(FK)    Degree   Module   Hall
----------------------------------
2020-11-18  CS       M1       H1
2020-11-19  SE       M2       H2
2020-11-20  CS       M3       H1

Auxiliary_Calculator

Date(PK)    KindOfDays
----------------------------------
2020-11-18  BankDay
2020-11-19  BankDay
2020-11-20  BankDay
2020-11-21  Holiday

The Date column of tb_exam is a foreign key to the Date column of Auxiliary_Calculator table. tb_exam的 Date 列是Auxiliary_Calculator表的 Date 列的外键。

I want to auto-increment the Date column in tb_exam table, and join the two tables to look something like this我想自动递增日期tb_exam表,并加入两个表是这个样子

Date(FK)    KindOfDays   Degree   Module   Hall
------------------------------------------------
2020-11-18  BankDay      CS       M1       H1
2020-11-19  BankDay      SE       M2       H2
2020-11-20  BankDay      CS       M3       H1

I tried incrementing this way.我尝试以这种方式递增。 But I don't know how to write the query to display the final table.但我不知道如何编写查询以显示最终表格。

CREATE PROCEDURE [dbo].[AutoIncrementDate]
@StartDate DATETIME,
@EndDate DATETIME
AS
WITH tb_exam
AS
(
SELECT DATEADD(D, DATEDIFF(D, 0, @StartDate), 0) AS Dt
UNION ALL
SELECT DATEADD(D, 1, Dt)
FROM tb_exam
WHERE Dt BETWEEN @StartDate AND DATEADD(D, -1, @EndDate)
)
SELECT *
FROM tb_exam
GO

Can anyone help me modify this?谁能帮我修改一下?

I have two SQL Server tables, tb_exam and Auxiliary_Calculator .我有两个 SQL Server 表, tb_examAuxiliary_Calculator

tb_exam

Date(FK)    Degree   Module   Hall
----------------------------------
2020-11-18  CS       M1       H1
2020-11-19  SE       M2       H2
2020-11-20  CS       M3       H1

Auxiliary_Calculator

Date(PK)    KindOfDays
----------------------------------
2020-11-18  BankDay
2020-11-19  BankDay
2020-11-20  BankDay
2020-11-21  Holiday

The Date column of tb_exam is a foreign key to the Date column of Auxiliary_Calculator table. tb_exam的 Date 列是Auxiliary_Calculator表的 Date 列的外键。

I want to auto-increment the Date column in tb_exam table, and join the two tables to look something like this我想自动递增日期tb_exam表,并加入两个表是这个样子

Date(FK)    KindOfDays   Degree   Module   Hall
------------------------------------------------
2020-11-18  BankDay      CS       M1       H1
2020-11-19  BankDay      SE       M2       H2
2020-11-20  BankDay      CS       M3       H1

I tried incrementing this way.我尝试以这种方式递增。 But I don't know how to write the query to display the final table.但我不知道如何编写查询以显示最终表格。

CREATE PROCEDURE [dbo].[AutoIncrementDate]
@StartDate DATETIME,
@EndDate DATETIME
AS
WITH tb_exam
AS
(
SELECT DATEADD(D, DATEDIFF(D, 0, @StartDate), 0) AS Dt
UNION ALL
SELECT DATEADD(D, 1, Dt)
FROM tb_exam
WHERE Dt BETWEEN @StartDate AND DATEADD(D, -1, @EndDate)
)
SELECT *
FROM tb_exam
GO

Can anyone help me modify this?谁能帮我修改一下?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM