I am writing a Stored Procedure. Basically i need take year and month information from user and doing something. But my problem is i don't detect given month has how many days. Let me explain with example;
User give me year and month as i said;
@year = 2015
@month = 07
So i must create rows like;
2015-07-01
2015-07-02
.....
2015-07-31
My plan is adding these rows one by one to another temp table.
Last status of my SP is;
ALTER PROCEDURE [dbo].[Usp_CreateStats]
(
@Year VARCHAR (40),
@Month VARCHAR (40)
)
AS
BEGIN
DECLARE @StartTime VARCHAR (10)
DECLARE @EndTime VARCHAR (10)
SET @StartTime = '00:00:00'
SET @EndTime = '23:59:59'
DECLARE @Peoples TABLE (name VARCHAR(50))
INSERT INTO @Peoples (name) select distinct name from USERINFO
DECLARE @Dates TABLE (date VARCHAR(50))
END
I hope explained correctly.
You can use recursive CTE.
Use Recursive CTE,
I've changed the year and month variables to
int
data type, the name of your date column todates_date
, and usedconvert
and a while loop withdateadd
function to populate the@dates
table:Here is another (faster) tally solution:
CTE you can use: