Home >Database >Mysql Tutorial >How to Generate a 100-Year Calendar Table in SQL?

How to Generate a 100-Year Calendar Table in SQL?

Barbara Streisand
Barbara StreisandOriginal
2025-01-21 01:27:10288browse

How to Generate a 100-Year Calendar Table in SQL?

Creating a 100-year calendar table in SQL Server

The following generic script creates a calendar table spanning 100 years in SQL Server:

<code class="language-sql">IF EXISTS (SELECT * FROM information_schema.tables WHERE Table_Name = 'Calendar' AND Table_Type = 'BASE TABLE')
BEGIN
    DROP TABLE [Calendar]
END

CREATE TABLE [Calendar] (
    [CalendarDate] DATETIME
)

DECLARE @StartDate DATETIME
DECLARE @EndDate DATETIME
SET @StartDate = GETDATE()
SET @EndDate = DATEADD(year, 100, @StartDate) -- 修改为100年

WHILE @StartDate <= @EndDate
BEGIN
    INSERT INTO [Calendar] (CalendarDate)
    SELECT @StartDate

    SET @StartDate = DATEADD(day, 1, @StartDate)
END</code>

Remember to adjust the start and end dates in the script to your specific needs. This code will generate a calendar table containing 100 years of data.

For more advanced calendar implementation, you can refer to other scripts that generate complete calendars with various attributes such as year, quarter, week, date, and holiday.

The above is the detailed content of How to Generate a 100-Year Calendar Table in SQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn