kreta/Kreta.DataAccess.Migrations/Scripts/Archive/20200824171433_DB_2675/generate_napok_hetek.sql
2024-03-13 00:33:46 +01:00

74 lines
No EOL
2 KiB
Transact-SQL

declare @c cursor,
@intezmenyId int,
@tanevId int,
@startDate datetime = '2020-09-01',
@endDate datetime = '2021-08-31',
@ABHet bit = 0;
declare @Dates table(Dates date not null primary key clustered);
WITH ctedaterange AS (
SELECT [Dates] = @startDate, week = 1
UNION ALL
SELECT [dates] + 1, week + IIF(DATEPART(dw, dates + 1) = 1, 1, 0)
FROM ctedaterange
WHERE [dates] + 1 <= @endDate
)
INSERT INTO @Dates(Dates)
SELECT s.Dates
FROM ctedaterange s
OPTION(maxrecursion 1000);
set @c = cursor static for
select t.ID, t.C_INTEZMENYID
from T_TANEV t
inner join T_INTEZMENY i on t.C_INTEZMENYID = i.ID
where t.C_NEV = '2020/2021'
and t.TOROLT = 'F'
and i.TOROLT = 'F'
order by t.ID;
open @c;
fetch next from @c into @tanevId, @intezmenyId;
while (@@fetch_status = 0)
begin
if exists(
select *
from T_DICTIONARYITEMBASE
where C_TYPE like 'HetiRendTipus'
and C_INTEZMENYID = @intezmenyId
and C_TANEVID = @tanevId
and ID <> 1554
)
set @ABHet = 1
else
set @ABHet = 0;
if exists(
select t.ID, T.C_INTEZMENYID, d.Dates, h.C_HETSORSZAMA
from T_TANEV t
inner join T_INTEZMENY i on i.ID = t.C_INTEZMENYID
inner join @Dates d on d.Dates between t.C_ELSOTANITASINAP and t.C_UTOLSOTANITASINAP
left join T_NAPTARIHET h on d.Dates between h.C_HETKEZDONAPJA and h.C_HETUTOLSONAPJA and h.C_TANEVID = t.ID
where t.ID = @tanevId
and i.ID = @intezmenyId
and t.TOROLT = 'F'
and i.TOROLT = 'F'
and h.C_HETSORSZAMA is null
)
exec dbo.sp_GenerateNaptariHetek @intezmenyId = @intezmenyId,
@tanevId = @tanevId,
@ABHet = @ABHet;
exec dbo.sp_GenerateNaptariNapok @intezmenyId = @intezmenyId,
@tanevId = @tanevId,
@overridedefault = 0;
print concat('@intezmenyId=', @intezmenyId, ', @tanevId=', @tanevId, ' @ABHet=', @ABHet)
fetch next from @c into @tanevId, @intezmenyId;
end
close @c;