SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

CREATE PROCEDURE [dbo].[sp_GetOsztalyTanuloinakHaviMulasztasaiOsszesitoje_honapra] 
  @pTanevId INT,
  @pIntezmenyId INT,
  @pOsztalyId INT,
  @pHonap INT,
  @pElmeletgyakorlat BIT = 0
AS 
BEGIN

--Osztály tanulói
CREATE TABLE #tanulo (Id INT)
INSERT INTO #tanulo
	SELECT DISTINCT 
		tcs.c_tanuloid AS Id 
  FROM T_TANULOCSOPORT_OSSZES tcs
	INNER JOIN T_FELHASZNALO_OSSZES f ON 
		f.id = tcs.c_tanuloid
	WHERE 
		tcs.torolt='F' AND 
		f.torolt='F' AND 
		tcs.c_tanevid=@pTanevId AND 
		tcs.c_intezmenyid=@pIntezmenyId AND 
		tcs.c_osztalycsoportid=@pOsztalyId;

--Igazolástípusok
CREATE TABLE #igazolastipus (Id INT, Nev NVARCHAR(MAX), Sorrend INT);
INSERT INTO #igazolastipus
SELECT 
	T_IGAZOLASTIPUS_OSSZES.ID AS Id, 
	C_NAME AS Nev, C_ORDER AS Sorrrend
FROM T_IGAZOLASTIPUS_OSSZES 
INNER JOIN T_DICTIONARYITEMBASE_OSSZES ON 
	T_IGAZOLASTIPUS_OSSZES.ID = T_DICTIONARYITEMBASE_OSSZES.ID
WHERE 
	T_DICTIONARYITEMBASE_OSSZES.C_INTEZMENYID = @pIntezmenyId AND 
	T_DICTIONARYITEMBASE_OSSZES.C_TANEVID = @pTanevId AND 
	T_IGAZOLASTIPUS_OSSZES.C_ALINTEZMENYID = @pIntezmenyId AND 
	T_IGAZOLASTIPUS_OSSZES.C_ALTANEVID = @pTanevId AND 
	T_IGAZOLASTIPUS_OSSZES.TOROLT = 'F' AND T_DICTIONARYITEMBASE_OSSZES.TOROLT = 'F'
ORDER BY C_ORDER, C_NAME;

--Igazolások és típusai
CREATE TABLE #igazolas (Tanulo INT, IgazoltE NVARCHAR(1), IgazolasTipus INT, Gyakorlati CHAR(1), MulasztasTipus INT, KesesPerc INT)
INSERT INTO #igazolas
	SELECT 
		tm.C_ORATANULOIID AS Tanulo, 
		tm.C_IGAZOLT AS IgazoltE, 
		tm.C_IGAZOLASTIPUSA AS IgazolasTipus, 
		IIF(@pElmeletgyakorlat=0, 'F', C_GYAKORLATI) AS Gyakorlati,
		tm.C_TIPUS AS MulasztasTipus,
		tm.C_KESESPERCBEN AS KesesPerc
	FROM T_TANULOMULASZTAS_OSSZES AS tm
	INNER JOIN T_TANITASIORA_OSSZES tao ON 
		tao.ID = tm.C_TANITASIORAKID
	INNER JOIN T_TANTARGY_OSSZES ON 
		T_TANTARGY_OSSZES.ID = tao.C_TANTARGYID AND 
		T_TANTARGY_OSSZES.C_TANEVID = tao.C_TANEVID
	WHERE 
		tao.C_INTEZMENYID = @pIntezmenyId AND 
		tm.C_INTEZMENYID = @pIntezmenyId AND 
		tm.C_TANEVID = @pTanevId AND 
		tao.C_TANEVID = @pTanevId AND 
		tm.TOROLT = 'F' AND 
		tao.TOROLT = 'F' AND 
		(CAST(MONTH(tao.C_DATUM) AS VARCHAR))=@pHonap;

--Mulasztások
CREATE TABLE #mulasztas (Nap NVARCHAR(5), Tanulo INT, Osztaly INT)
INSERT INTO #mulasztas
	SELECT 
		CAST(CAST(DAY(tao.C_DATUM) AS INT) AS NVARCHAR) + (IIF(@pElmeletgyakorlat=0, '', IIF(C_GYAKORLATI = 'T', '_GY', '_E'))) AS Nap, 
		tm.C_ORATANULOIID AS Tanulo, 
		tao.C_OSZTALYCSOPORTID AS Osztaly 
	FROM T_TANULOMULASZTAS_OSSZES tm
	INNER JOIN T_TANITASIORA_OSSZES tao ON 
		tao.id=tm.C_TANITASIORAKID 
	INNER JOIN T_TANTARGY_OSSZES ON 
		T_TANTARGY_OSSZES.ID = tao.C_TANTARGYID AND 
		T_TANTARGY_OSSZES.C_TANEVID = tao.C_TANEVID
	WHERE 
		tao.C_INTEZMENYID = @pIntezmenyId AND 
		tm.C_INTEZMENYID = @pIntezmenyId AND 
		tm.C_TANEVID = @pTanevId AND 
		tao.C_TANEVID = @pTanevId AND 
		tm.TOROLT = 'F' AND 
		tao.TOROLT = 'F' AND 
		(CAST(MONTH(tao.C_DATUM) AS VARCHAR))=@pHonap;

DECLARE @query AS NVARCHAR(MAX);
IF @pElmeletgyakorlat=0
    BEGIN
    DECLARE @colsHeader AS NVARCHAR(MAX);
    DECLARE @cols AS NVARCHAR(MAX);
    SELECT @colsHeader = ISNULL(@colsHeader + ', ', '') + QUOTENAME(Id) + ' AS ' + QUOTENAME(Nev),
			     @cols = ISNULL(@cols + ', ', '') + QUOTENAME(Id)
    FROM #igazolastipus AS Igazolastipus
    ORDER BY Sorrend, Nev;
    SET @query = 
    'SELECT f.c_nyomtatasinev AS COLUMN108, 
      IIF(m.[1]=0, NULL, m.[1]) AS [1], IIF(m.[2]=0, NULL, m.[2]) AS [2], IIF(m.[3]=0, NULL, m.[3]) AS [3], IIF(m.[4]=0, NULL, m.[4]) AS [4], IIF(m.[5]=0, NULL, m.[5]) AS [5], IIF(m.[6]=0, NULL, m.[6]) AS [6], IIF(m.[7]=0, NULL, m.[7]) AS [7], 
      IIF(m.[8]=0, NULL, m.[8]) AS [8], IIF(m.[9]=0, NULL, m.[9]) AS [9], IIF(m.[10]=0, NULL, m.[10]) AS [10], IIF(m.[11]=0, NULL, m.[11]) AS [11], IIF(m.[12]=0, NULL, m.[12]) AS [12], IIF(m.[13]=0, NULL, m.[13]) AS [13], IIF(m.[14]=0, NULL, m.[14]) AS [14], 
      IIF(m.[15]=0, NULL, m.[15]) AS [15], IIF(m.[16]=0, NULL, m.[16]) AS [16], IIF(m.[17]=0, NULL, m.[17]) AS [17], IIF(m.[18]=0, NULL, m.[18]) AS [18], IIF(m.[19]=0, NULL, m.[19]) AS [19], IIF(m.[20]=0, NULL, m.[20]) AS [20], IIF(m.[21]=0, NULL, m.[21]) AS [21], 
      IIF(m.[22]=0, NULL, m.[22]) AS [22], IIF(m.[23]=0, NULL, m.[23]) AS [23], IIF(m.[24]=0, NULL, m.[24]) AS [24], IIF(m.[25]=0, NULL, m.[25]) AS [25], IIF(m.[26]=0, NULL, m.[26]) AS [26], IIF(m.[27]=0, NULL, m.[27]) AS [27], IIF(m.[28]=0, NULL, m.[28]) AS [28], 
      IIF(m.[29]=0, NULL, m.[29]) AS [29], IIF(m.[30]=0, NULL, m.[30]) AS [30], IIF(m.[31]=0, NULL, m.[31]) AS [31], 
      OSSZES.MULASZTASOKSZAMA AS COLUMN2672, IGAZOLT.IGAZOLTSZAMA AS COLUMN2568, ' + @colsHeader + ', IGAZOLATLAN.IGAZOLATLANSZAMA AS COLUMN1911
     FROM #tanulo AS tanulok
     LEFT JOIN (
       SELECT *
       FROM #igazolas AS igazolasok
       PIVOT ( COUNT(IgazoltE) FOR IgazolasTipus IN (' + @cols + ')) AS PIVOTTABLE
     ) AS IGAZOLASTIPUSOKSZERINT ON IGAZOLASTIPUSOKSZERINT.Tanulo = tanulok.Id
     LEFT JOIN (
      SELECT *
      FROM #mulasztas AS m 
      PIVOT (COUNT(Osztaly) for Nap in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10], [11], [12], [13], [14], [15], [16], [17], [18], [19], [20], [21], [22], [23], [24], [25], [26], [27], [28], [29], [30], [31])) pv
     ) m ON m.Tanulo = tanulok.id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS MULASZTASOKSZAMA FROM #igazolas GROUP BY Tanulo) AS OSSZES ON OSSZES.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLTSZAMA FROM #igazolas i WHERE i.IgazoltE=''T'' GROUP BY Tanulo) AS IGAZOLT ON IGAZOLT.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLATLANSZAMA FROM #igazolas i WHERE i.IgazoltE=''F'' GROUP BY Tanulo) AS IGAZOLATLAN ON IGAZOLATLAN.Tanulo = tanulok.Id
     INNER JOIN T_FELHASZNALO_OSSZES f ON f.id = tanulok.Id
     ORDER BY f.c_nyomtatasinev';
    execute(@query);
  END
ELSE
  BEGIN
    DECLARE @colsHeader_E AS NVARCHAR(MAX);
    DECLARE @colsHeader_GY AS NVARCHAR(MAX);
    DECLARE @cols_E AS NVARCHAR(MAX);
    DECLARE @cols_GY AS NVARCHAR(MAX);
    SELECT @colsHeader_E = ISNULL(@colsHeader_E + ', ', '') + QUOTENAME(Id) + ' AS ' + QUOTENAME(Nev + ' (elméleti)'), @colsHeader_GY = ISNULL(@colsHeader_GY + ', ', '') + QUOTENAME(-Id) + ' AS ' + QUOTENAME(Nev + ' (gyakorlati)'),
			     @cols_E = ISNULL(@cols_E + ', ', '') + QUOTENAME(Id), @cols_GY = ISNULL(@cols_GY + ', ', '') + QUOTENAME(-Id)
    FROM #igazolastipus AS Igazolastipus
    ORDER BY Sorrend, Nev;
  
    SET @query = 
    'SELECT f.c_nyomtatasinev AS COLUMN108, 
      IIF(m.[1_E]=0, NULL, m.[1_E]) AS [1 E], IIF(m.[1_GY]=0, NULL, m.[1_GY]) AS [1 GY], IIF(m.[2_E]=0, NULL, m.[2_E]) AS [2 E], IIF(m.[2_GY]=0, NULL, m.[2_GY]) AS [2 GY], IIF(m.[3_E]=0, NULL, m.[3_E]) AS [3 E], IIF(m.[3_GY]=0, NULL, m.[3_GY]) AS [3 GY], 
      IIF(m.[4_E]=0, NULL, m.[4_E]) AS [4 E], IIF(m.[4_GY]=0, NULL, m.[4_GY]) AS [4 GY], IIF(m.[5_E]=0, NULL, m.[5_E]) AS [5 E], IIF(m.[5_GY]=0, NULL, m.[5_GY]) AS [5 GY], IIF(m.[6_E]=0, NULL, m.[6_E]) AS [6 E], IIF(m.[6_GY]=0, NULL, m.[6_GY]) AS [6 GY], 
      IIF(m.[7_E]=0, NULL, m.[7_E]) AS [7 E], IIF(m.[7_GY]=0, NULL, m.[7_GY]) AS [7 GY], IIF(m.[8_E]=0, NULL, m.[8_E]) AS [8 E], IIF(m.[8_GY]=0, NULL, m.[8_GY]) AS [8 GY], IIF(m.[9_E]=0, NULL, m.[9_E]) AS [9 E], IIF(m.[9_GY]=0, NULL, m.[9_GY]) AS [9 GY], 
      IIF(m.[10_E]=0, NULL, m.[10_E]) AS [10 E], IIF(m.[10_GY]=0, NULL, m.[10_GY]) AS [10 GY], IIF(m.[11_E]=0, NULL, m.[11_E]) AS [11 E], IIF(m.[11_GY]=0, NULL, m.[11_GY]) AS [11 GY], IIF(m.[12_E]=0, NULL, m.[12_E]) AS [12 E], IIF(m.[12_GY]=0, NULL, m.[12_GY]) AS [12 GY], 
      IIF(m.[13_E]=0, NULL, m.[13_E]) AS [13 E], IIF(m.[13_GY]=0, NULL, m.[13_GY]) AS [13 GY], IIF(m.[14_E]=0, NULL, m.[14_E]) AS [14 E], IIF(m.[14_GY]=0, NULL, m.[14_GY]) AS [14 GY], IIF(m.[15_E]=0, NULL, m.[15_E]) AS [15 E], IIF(m.[15_GY]=0, NULL, m.[15_GY]) AS [15 GY], 
      IIF(m.[16_E]=0, NULL, m.[16_E]) AS [16 E], IIF(m.[16_GY]=0, NULL, m.[16_GY]) AS [16 GY], IIF(m.[17_E]=0, NULL, m.[17_E]) AS [17 E], IIF(m.[17_GY]=0, NULL, m.[17_GY]) AS [17 GY], IIF(m.[18_E]=0, NULL, m.[18_E]) AS [18 E], IIF(m.[18_GY]=0, NULL, m.[18_GY]) AS [18 GY], 
      IIF(m.[19_E]=0, NULL, m.[19_E]) AS [19 E], IIF(m.[19_GY]=0, NULL, m.[19_GY]) AS [19 GY], IIF(m.[20_E]=0, NULL, m.[20_E]) AS [20 E], IIF(m.[20_GY]=0, NULL, m.[20_GY]) AS [20 GY], IIF(m.[21_E]=0, NULL, m.[21_E]) AS [21 E], IIF(m.[21_GY]=0, NULL, m.[21_GY]) AS [21 GY], 
      IIF(m.[22_E]=0, NULL, m.[22_E]) AS [22 E], IIF(m.[22_GY]=0, NULL, m.[22_GY]) AS [22 GY], IIF(m.[23_E]=0, NULL, m.[23_E]) AS [23 E], IIF(m.[23_GY]=0, NULL, m.[23_GY]) AS [23 GY], IIF(m.[24_E]=0, NULL, m.[24_E]) AS [24 E], IIF(m.[24_GY]=0, NULL, m.[24_GY]) AS [24 GY], 
      IIF(m.[25_E]=0, NULL, m.[25_E]) AS [25 E], IIF(m.[25_GY]=0, NULL, m.[25_GY]) AS [25 GY], IIF(m.[26_E]=0, NULL, m.[26_E]) AS [26 E], IIF(m.[26_GY]=0, NULL, m.[26_GY]) AS [26 GY], IIF(m.[27_E]=0, NULL, m.[27_E]) AS [27 E], IIF(m.[27_GY]=0, NULL, m.[27_GY]) AS [27 GY], 
      IIF(m.[28_E]=0, NULL, m.[28_E]) AS [28 E], IIF(m.[28_GY]=0, NULL, m.[28_GY]) AS [28 GY], IIF(m.[29_E]=0, NULL, m.[29_E]) AS [29 E], IIF(m.[29_GY]=0, NULL, m.[29_GY]) AS [29 GY], IIF(m.[30_E]=0, NULL, m.[30_E]) AS [30 E], IIF(m.[30_GY]=0, NULL, m.[30_GY]) AS [30 GY], 
      IIF(m.[31_E]=0, NULL, m.[31_E]) AS [31 E], IIF(m.[31_GY]=0, NULL, m.[31_GY]) AS [31 GY], 
      (osszes_e.MULASZTASOKSZAMA + FLOOR(osszes_e_keses.MULASZTASOKSZAMA / 45)) AS COLUMN2672_E, 
	  (igazolt_e.IGAZOLTSZAMA + FLOOR(igazolt_e_keses.IGAZOLTSZAMA / 45)) AS COLUMN2568_E, 
	  ' + @colsHeader_E + ', 
	  (igazolatlan_e.IGAZOLATLANSZAMA + FLOOR(igazolatlan_e_keses.IGAZOLATLANSZAMA / 45)) AS COLUMN1911_E, 
      (osszes_gy.MULASZTASOKSZAMA + FLOOR(osszes_gy_keses.MULASZTASOKSZAMA / 60)) AS COLUMN2672_GY, 
	  (igazolt_gy.IGAZOLTSZAMA + FLOOR(iigazolt_gy_keses.IGAZOLTSZAMA / 60)) AS COLUMN2568_GY, 
	  ' + @colsHeader_GY + ', 
	  (igazolatlan_gy.IGAZOLATLANSZAMA + FLOOR(igazolatlan_gy_keses.IGAZOLATLANSZAMA / 60)) AS COLUMN1911_GY
     FROM #tanulo AS tanulok
     LEFT JOIN (
       SELECT * FROM (
         SELECT *
         FROM #igazolas AS igazolasok
         WHERE Gyakorlati = ''F'' OR Gyakorlati IS NULL
       ) AS igazolasok
       PIVOT ( COUNT(IgazoltE) FOR IgazolasTipus IN (' + @cols_E + ')) AS PIVOTTABLE_E
     ) AS IGAZOLASTIPUSOKSZERINT_E ON IGAZOLASTIPUSOKSZERINT_E.Tanulo = tanulok.Id
     LEFT JOIN (
       SELECT * FROM (
         SELECT Tanulo, IgazoltE, -IgazolasTipus AS IgazolasTipus,  Gyakorlati
         FROM #igazolas AS igazolasok
         WHERE Gyakorlati = ''T''
       ) AS igazolasok
       PIVOT ( COUNT(IgazoltE) FOR IgazolasTipus IN (' + @cols_GY + ')) AS PIVOTTABLE_GY
     ) AS IGAZOLASTIPUSOKSZERINT_GY ON IGAZOLASTIPUSOKSZERINT_GY.Tanulo = tanulok.Id
     LEFT JOIN (
      SELECT * FROM #mulasztas AS m 
      PIVOT (COUNT(Osztaly) for Nap in (
        [1_E],[2_E],[3_E],[4_E],[5_E],[6_E],[7_E],[8_E],[9_E],[10_E],[11_E],[12_E],[13_E],[14_E],[15_E],[16_E],[17_E],[18_E],[19_E],[20_E],[21_E],[22_E],[23_E],[24_E],[25_E],[26_E],[27_E],[28_E],[29_E],[30_E],[31_E],
        [1_GY],[2_GY],[3_GY],[4_GY],[5_GY],[6_GY],[7_GY],[8_GY],[9_GY],[10_GY],[11_GY],[12_GY],[13_GY],[14_GY],[15_GY],[16_GY],[17_GY],[18_GY],[19_GY],[20_GY],[21_GY],[22_GY],[23_GY],[24_GY],[25_GY],[26_GY],[27_GY],[28_GY],[29_GY],[30_GY],[31_GY]
     )) pv) m ON m.Tanulo = tanulok.id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS MULASZTASOKSZAMA FROM #igazolas WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Osszes_E ON Osszes_E.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLTSZAMA FROM #igazolas i WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND i.IgazoltE=''T'' AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Igazolt_E ON Igazolt_E.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLATLANSZAMA FROM #igazolas i WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND  i.IgazoltE=''F'' AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Igazolatlan_E ON Igazolatlan_E.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS MULASZTASOKSZAMA FROM #igazolas WHERE Gyakorlati = ''T'' AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Osszes_Gy ON Osszes_Gy.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLTSZAMA FROM #igazolas i WHERE Gyakorlati = ''T'' AND i.IgazoltE=''T'' AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Igazolt_Gy ON Igazolt_Gy.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, COUNT(Tanulo) AS IGAZOLATLANSZAMA FROM #igazolas i WHERE Gyakorlati = ''T'' AND  i.IgazoltE=''F'' AND MulasztasTipus = 1500 GROUP BY Tanulo) AS Igazolatlan_Gy ON Igazolatlan_Gy.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS MULASZTASOKSZAMA FROM #igazolas WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Osszes_E_Keses ON Osszes_E_Keses.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS IGAZOLTSZAMA FROM #igazolas i WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND i.IgazoltE=''T'' AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Igazolt_E_Keses ON Igazolt_E_Keses.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS IGAZOLATLANSZAMA FROM #igazolas i WHERE (Gyakorlati = ''F'' OR Gyakorlati IS NULL) AND  i.IgazoltE=''F'' AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Igazolatlan_E_Keses ON Igazolatlan_E_Keses.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS MULASZTASOKSZAMA FROM #igazolas WHERE Gyakorlati = ''T'' AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Osszes_Gy ON Osszes_Gy_Keses.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS IGAZOLTSZAMA FROM #igazolas i WHERE Gyakorlati = ''T'' AND i.IgazoltE=''T'' AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Igazolt_Gy_Keses ON Igazolt_Gy_Keses.Tanulo = tanulok.Id
     LEFT JOIN (SELECT Tanulo, SUM(KesesPerc) AS IGAZOLATLANSZAMA FROM #igazolas i WHERE Gyakorlati = ''T'' AND  i.IgazoltE=''F'' AND MulasztasTipus = 1499 GROUP BY Tanulo) AS Igazolatlan_Gy_Keses ON Igazolatlan_Gy_Keses.Tanulo = tanulok.Id
	 INNER JOIN T_FELHASZNALO_OSSZES f ON f.id = tanulok.Id
     ORDER BY f.c_nyomtatasinev';
    execute(@query);
  END
DROP TABLE #mulasztas
DROP TABLE #tanulo
DROP TABLE #igazolas
DROP TABLE #igazolastipus
END 

GO