DROP PROCEDURE IF EXISTS uspListTanariHaziFeladatIdByOraId
GO

CREATE PROCEDURE uspListTanariHaziFeladatIdByOraId
  @pOrarendiOraItemsListString  nvarchar(max) = NULL
 ,@pTanitasiOraIdsListString    nvarchar(max) = NULL
AS
BEGIN
  SET NOCOUNT ON;
  DECLARE @sql nvarchar(max)
  SET @sql = N'
    SELECT
         haziFeladat.ID HaziFeladatId
        ,tanitasiOra.ID TanitasiOraId
        ,orarendiOra.ID OrarendiOraId
        ,haziFeladat.C_DATUM OraDatuma
        ,haziFeladat.C_LATHATOSAGIDOPONT LathatosagIdopont
    FROM        T_DKT_FELADAT haziFeladat
    LEFT JOIN   T_TANITASIORA tanitasiOra ON tanitasiOra.ID = haziFeladat.C_TANITASIORAID AND tanitasiOra.TOROLT = ''F''
    LEFT JOIN   T_ORARENDIORA orarendiOra ON orarendiOra.C_ORARENDIORAGROUPID = haziFeladat.C_ORARENDIORAGROUPID AND
                                                haziFeladat.C_DATUM >= orarendiOra.C_ORAERVENYESSEGKEZDETE AND
                                                (haziFeladat.C_DATUM < orarendiOra.C_ORAERVENYESSEGVEGE OR (haziFeladat.C_DATUM = orarendiOra.C_ORAERVENYESSEGVEGE AND haziFeladat.C_DATUM = orarendiOra.C_ORAERVENYESSEGKEZDETE)) AND
                                                orarendiOra.TOROLT = ''F''
    WHERE  haziFeladat.C_FELADATTIPUSID = 0 AND
           haziFeladat.TOROLT = ''F'' AND' +
           IIF(@pOrarendiOraItemsListString IS NOT NULL,
                ' (EXISTS (SELECT 1 FROM T_ORARENDIORA WHERE ' + @pOrarendiOraItemsListString + ') OR tanitasiOra.ID IN (SELECT value FROM STRING_SPLIT(@pTanitasiOraIdsListString, '','')))',
                ' tanitasiOra.ID IN (SELECT value FROM STRING_SPLIT(@pTanitasiOraIdsListString, '',''))')

EXEC sp_executesql @sql, N'
    @pTanitasiOraIdsListString nvarchar(max) '
   ,@pTanitasiOraIdsListString = @pTanitasiOraIdsListString
END 
GO

