SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('[dbo].[sp_GetOsztalyTanuloinakHaviMulasztasaiOsszesitoje]') IS NOT NULL 
BEGIN
  DROP PROCEDURE [dbo].[sp_GetOsztalyTanuloinakHaviMulasztasaiOsszesitoje]
END
GO

CREATE PROCEDURE [dbo].[sp_GetOsztalyTanuloinakHaviMulasztasaiOsszesitoje] 
  @pTanevId INT,
  @pIntezmenyId INT, 
  @pOsztalyId INT,
  @pElmeletgyakorlat BIT = 0
AS 
BEGIN
DECLARE @honapok TABLE(honap INT, honapnev NVARCHAR(max));
INSERT INTO @honapok
SELECT DISTINCT (3 + d.c_value) % 12 + 1 AS c_value, d.c_name as Honap 
FROM T_DICTIONARYITEMBASE_OSSZES d 
WHERE c_type LIKE 'honap%' 
ORDER BY c_value;

SELECT honapnev FROM @honapok;

DECLARE honap_cursor CURSOR FOR 
SELECT DISTINCT (3 + d.c_value) % 12 + 1 AS honap_sorrend, d.c_value AS honap
FROM T_DICTIONARYITEMBASE_OSSZES d 
WHERE c_type LIKE 'honap%'
ORDER BY honap_sorrend;

DECLARE @honap_sorrend INT, @honap INT;
OPEN honap_cursor
FETCH NEXT FROM honap_cursor 
INTO @honap_sorrend, @honap;

WHILE @@FETCH_STATUS = 0 
BEGIN
  EXEC [sp_GetOsztalyTanuloinakHaviMulasztasaiOsszesitoje_honapra] @pTanevId, @pIntezmenyId, @pOsztalyId, @honap, @pElmeletgyakorlat
  FETCH NEXT FROM honap_cursor 
  INTO @honap_sorrend, @honap
END
CLOSE honap_cursor
DEALLOCATE honap_cursor
END

GO