210k views
2 votes
Create a table in your own database using the following statement.

CREATE TABLE DateRange
(DateID INT IDENTITY,
DateValue DATE,
DayOfWeek SMALLINT,
Week SMALLINT,
Month SMALLINT,
Quarter SMALLINT,
Year SMALLINT
);

Write a stored procedure that accepts two parameters:
1) A starting date
2) The number of the consecutive dates beginning with the starting date
The stored procedure then inserts data into all columns of the DateRange table according to the two provided parameters.

User Kolaente
by
4.5k points

1 Answer

1 vote

Answer:

Check the explanation

Step-by-step explanation:

CREATE FUNCTION dbo.DateRange_sp4 ("at"StartDate DATE, "at"NumberofConsecutivedays INT) RETURNS "at"DateList TABLE ( DateID INT IDENTITY, DateValue DATE, Year SMALLINT, Quarter SMALLINT, Month SMALLINT, Week SMALLINT, DayOfWeek SMALLINT ) AS BEGIN DECLARE "at"Counter INT = 0; WHILE ("at"Counter < "at"NumberofConsecutivedays) BEGIN INSERT INTO "at"DateList VALUES ("at"Counter + 1, DATEADD(DAY, "at"Counter, "at"StartDate), DATEPART(YEAR, "at"StartDate), DATEPART(QUARTER, "at"StartDate), DATEPART(MONTH, "at"StartDate), DATEPART(WEEK, "at"StartDate), DatePart(WEEKDAY, "at"StartDate) ); SET "at"StartDate = DATEADD(day,"at"Counter + 1, "at"StartDate); SET "at"Counter += 1 END RETURN; END GO SELECT * FROM dbo.DateRange_sp4('2020-01-10', 20);

kindly check the screenshot below

Create a table in your own database using the following statement. CREATE TABLE DateRange-example-1
User Geekinit
by
4.4k points