简体   繁体   中英

How to auto-increment dates in Sql Server?

I have two SQL Server tables, tb_exam and Auxiliary_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.

I want to auto-increment the Date column in tb_exam table, and join the two tables to look something like this

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 .

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.

I want to auto-increment the Date column in tb_exam table, and join the two tables to look something like this

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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