DROP PROCEDURE IF EXISTS uspGetPedagogusTeremFogadoora
GO

CREATE PROCEDURE uspGetPedagogusTeremFogadoora
   @pTanevId int
   ,@pIdoszakKezdete datetime
   ,@pIdoszakVege datetime
   ,@pTanarId int = NULL
   ,@pTeremId int = NULL
AS
BEGIN
  SET NOCOUNT ON

  DECLARE @sql nvarchar(max)

  SET @sql = N'
  SELECT 
     ID
    ,C_EGYSEG AS Egyseg
    ,C_FOGADOORAKEZDETE AS FogadooraKezdete
    ,C_FOGADOORATIPUSID AS FogadooraTipusId
    ,C_FOGADOORAVEGE AS FogadooraVege
    ,C_GROUPID AS GroupId
    ,C_RENDSZERESSEG AS Rendszeresseg
    ,C_NEMKOTOTTMUNKAIDOID AS NemKotottMunkaidoId
    ,C_TANARID AS TanarId
    ,C_TEREMID AS TeremId
    ,C_TANULOESEMENYID AS TanuloEsemenyId
  FROM T_FOGADOORA_OSSZES fo
  WHERE fo.C_FOGADOORAVEGE > @pIdoszakKezdete
    AND fo.C_FOGADOORAKEZDETE < @pIdoszakVege
    AND fo.C_TANEVID = @pTanevId
    AND fo.TOROLT = ''F'''
  
  SET @sql += IIF(@pTanarId IS NOT NULL, N'
    AND fo.C_TANARID = @pTanarId', '')
  SET @sql += IIF(@pTeremId IS NOT NULL, N'
    AND fo.C_TEREMID = @pTeremId', '')

  EXEC sp_executesql @sql, N'
   @pIdoszakKezdete datetime
  ,@pIdoszakVege datetime
  ,@pTanevId int
  ,@pTanarId int
  ,@pTeremId int'
  ,@pIdoszakKezdete = @pIdoszakKezdete
  ,@pIdoszakVege = @pIdoszakVege
  ,@pTanevId = @pTanevId
  ,@pTanarId = @pTanarId
  ,@pTeremId = @pTeremId
END
GO