Pessoal, boa noite. Como eu utilizo o script do professor somente para gerar informações de Semestre e Ano? Eu mexi no declare e no insert para somente esses dados, porém ainda estou com problemas, pois é gerado mais valores do que eu preciso. Segue script abaixo:
DECLARE @DIMDATE TABLE
(
[COD_TEMPO] INT PRIMARY KEY IDENTITY (1,1),
[COD_SEMESTRE] NVARCHAR(50),
[DESC_SEMESTRE] NVARCHAR(50),
[COD_SEMESTRE_ANO] NVARCHAR(50),
[DESC_SEMESTRE_ANO] NVARCHAR(50),
[ANO] NVARCHAR(50)
)
DECLARE @AnoInicial VARCHAR(4) = '2014'
DECLARE @MesInicial VARCHAR(2) = '1'
DECLARE @AnoFinal VARCHAR(4) = '2020'
DECLARE @MesFinal VARCHAR(2) = '6'
DECLARE @StartDate DATETIME
Select @StartDate = CAST(@AnoInicial + '/' + @MesInicial + '/01' AS DATETIME)
DECLARE @EndDate DATETIME
SELECT @EndDate = DATEADD(month, ((CAST(@AnoFinal AS INTEGER) - 1900) * 12) + CAST(@MesFinal AS INTEGER), 0)
DECLARE
@DayOfWeekInMonth INT,
@DayOfWeekInYear INT,
@DayOfQuarter INT,
@WeekOfMonth INT,
@CurrentYear INT,
@CurrentMonth INT,
@CurrentQuarter INT
DECLARE @DayOfWeek TABLE (DOW INT, MonthCount INT, QuarterCount INT, YearCount INT)
INSERT INTO @DayOfWeek VALUES (1, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (2, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (3, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (4, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (5, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (6, 0, 0, 0)
INSERT INTO @DayOfWeek VALUES (7, 0, 0, 0)
DECLARE @CurrentDate AS DATETIME = @StartDate
SET @CurrentMonth = DATEPART(MM, @CurrentDate)
SET @CurrentYear = DATEPART(YY, @CurrentDate)
SET @CurrentQuarter = DATEPART(QQ, @CurrentDate)
WHILE @CurrentDate < @EndDate
BEGIN
IF @CurrentMonth != DATEPART(MM, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET MonthCount = 0
SET @CurrentMonth = DATEPART(MM, @CurrentDate)
END
IF @CurrentQuarter != DATEPART(QQ, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET QuarterCount = 0
SET @CurrentQuarter = DATEPART(QQ, @CurrentDate)
END
IF @CurrentYear != DATEPART(YY, @CurrentDate)
BEGIN
UPDATE @DayOfWeek
SET YearCount = 0
SET @CurrentYear = DATEPART(YY, @CurrentDate)
END
UPDATE @DayOfWeek
SET
MonthCount = MonthCount + 1,
QuarterCount = QuarterCount + 1,
YearCount = YearCount + 1
WHERE DOW = DATEPART(DW, @CurrentDate)
SELECT
@DayOfWeekInMonth = MonthCount,
@DayOfQuarter = QuarterCount,
@DayOfWeekInYear = YearCount
FROM @DayOfWeek
WHERE DOW = DATEPART(DW, @CurrentDate)
INSERT INTO @DIMDATE
SELECT
CASE DATEPART(QQ, @CurrentDate)
WHEN 1 THEN '01'
WHEN 2 THEN '01'
WHEN 3 THEN '02'
WHEN 4 THEN '02'
END
AS COD_SEMESTRE,
CASE DATEPART(QQ, @CurrentDate)
WHEN 1 THEN 'Primeiro Semestre'
WHEN 2 THEN 'Primeiro Semestre'
WHEN 3 THEN 'Segundo Semestre'
WHEN 4 THEN 'Segundo Semestre'
END
AS DESC_SEMESTRE,
CASE DATEPART(QQ, @CurrentDate)
WHEN 1 THEN '01'
WHEN 2 THEN '01'
WHEN 3 THEN '02'
WHEN 4 THEN '02'
END + '-' +
CONVERT(NVARCHAR(4), DATEPART(YEAR, @CurrentDate)) as COD_SEMESTRE_ANO,
CASE DATEPART(QQ, @CurrentDate)
WHEN 1 THEN 'Primeiro Semestre'
WHEN 2 THEN 'Primeiro Semestre'
WHEN 3 THEN 'Segundo Semestre'
WHEN 4 THEN 'Segundo Semestre'
END + ' ' +
CONVERT(NVARCHAR(4), DATEPART(YEAR, @CurrentDate)) as DESC_SEMESTRE_ANO,
CONVERT(NVARCHAR(4), DATEPART(YEAR, @CurrentDate)) AS ANO
SET @CurrentDate = DATEADD(DD, 1, @CurrentDate)
END
SELECT * FROM @DIMDATE order by COD_TEMPO
Poderiam me ajudar?