CREATE PROCEDURE sp_Attendance @Start DATETIME, @End DATETIME
AS
BEGIN
DECLARE @NumDays INT;
-- This will give you the number of days between your start date and end date.
SELECT @NumDays = DATEDIFF(DAY, @Start, @End) + 1;
WITH CTE AS (
SELECT TOP (@Numdays)
/*
ROW_NUMBER() OVER (ORDER BY a.object_id) will give you an integer from 1 to @NumDays becuase of TOP (@NumDays)
ROW_NUMBER() OVER (ORDER BY a.object_id) - 1 will give you an integer from 0 to @NumDays
DATEADD(DAY, ROW_NUMBER(), @Start) -- This will add the integer from the row number statement to your start date.
i.e.
@Start + 0
@Start + 1
@Start + 2
etc
etc
@Start + @NumDays
*/
DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY a.object_id) - 1, @Start) AS AttdDate
FROM
sys.all_columns a
CROSS JOIN
sys.all_columns b)
SELECT
c.AttdDate,
CASE WHEN a.AttdDate IS NOT NULL THEN 1 ELSE 0 END AS Status
FROM
CTE c
LEFT JOIN
Attendance a
ON c.AttdDate = a.AttdDate;
END;
これは素晴らしいです。本当にあなたは私の時間を節約しました。ありがとうございました – prime
本当に感謝しています。 1、@start) FROM AttdDate AS CROSSをsys.all_columns - しかし、CTE AS( SELECTのTOP(@Numdays)a.object_id BY DATEADD(DAY、ROW_NUMBER()OVER(ORDER)WITHこの部分を「説明してくださいJOIN sys.all_columns b) " – prime
説明に役立つコメントをコードに追加しました。 –