﻿DROP PROCEDURE IF EXISTS dbo.uspGetFelhasznaloEmailNelkulJelszoResethez
GO

CREATE PROCEDURE dbo.uspGetFelhasznaloEmailNelkulJelszoResethez
   @pTanevId INT
  ,@pTanuloIdList NVARCHAR(MAX) = NULL
AS
BEGIN
  SET NOCOUNT ON;

  DECLARE @sql NVARCHAR(MAX)
  SET @sql = N'
    SELECT
       f.C_NYOMTATASINEV
      ,f.C_OKTATASIAZONOSITO
    FROM T_FELHASZNALO_OSSZES f'

  IF @pTanuloIdList IS NOT NULL
  BEGIN
    SET @sql += N'
      INNER JOIN (SELECT value FROM STRING_SPLIT(@pTanuloIdList, '','')) felhasznalok ON felhasznalok.value = f.ID'
  END

  SET @sql += N'
      LEFT JOIN T_EMAIL_OSSZES email ON email.C_FELHASZNALOID = f.ID
        AND email.TOROLT = ''F''
        AND email.C_ISHIBASANMEGADVA = ''F''
        AND email.C_ALAPERTELMEZETT = ''T''
        AND email.C_GONDVISELOID IS NULL
    WHERE f.TOROLT = ''F''
      AND email.ID IS NULL
      AND f.C_TANEVID = @pTanevId'

  EXEC sp_executesql
     @sql
    ,N'@pTanevId INT
      ,@pTanuloIdList NVARCHAR(MAX)'
    ,@pTanevId = @pTanevId
    ,@pTanuloIdList = @pTanuloIdList

END
GO