DROP PROCEDURE IF EXISTS uspGetTanarId

GO
  CREATE PROCEDURE uspGetTanarId
      @pDatum DATETIME
     ,@pOraszam INT = NULL
     ,@pOsztalyCsoportId INT
     ,@pTantargyId INT

  AS
  BEGIN
    SET NOCOUNT ON;

  SELECT oora.C_TANARID AS TanarId
  FROM T_ORARENDTELJES oora WITH(NOLOCK)
  WHERE oora.C_TANTARGYID = @pTantargyId
    AND oora.C_OSZTALYCSOPORTID = @pOsztalyCsoportId
    AND (@pOraszam IS NULL OR oora.C_ORASZAM = @pOraszam)
    AND oora.C_DATUM = CONVERT(date, @pDatum)
    AND oora.C_ORAKEZDETE = CONVERT(time, @pDatum)
  UNION ALL
  SELECT tora.C_TANARID AS TanarId
  FROM T_TANITASIORA tora WITH(NOLOCK)
  WHERE tora.C_TANTARGYID = @pTantargyId
    AND tora.C_OSZTALYCSOPORTID = @pOsztalyCsoportId
    AND (@pOraszam IS NULL OR tora.C_ORASZAM = @pOraszam)
    AND tora.C_ORAKEZDETE = @pDatum

  END
GO