
	IF OBJECT_ID('NET_SEL_PARTICIPANTEEVENTO') IS NOT NULL
		DROP PROC NET_SEL_PARTICIPANTEEVENTO

	GO
		CREATE PROC NET_SEL_PARTICIPANTEEVENTO
	(
	  @pCodEvento INT = NULL
	, @pNome VARCHAR(255)
	, @pEmail VARCHAR(255) 
	, @pStatus INT = NULL
    ,@pOrder VARCHAR(50) =  NULL 
	)
	AS 
		
		BEGIN
			SET NOCOUNT ON
			
					SELECT 
								  A.CodParticipante
							    , A.CodResponsavel   
								, A.Nome   
								, A.Email   
								, case When  A.Status is null Then  3 Else A.Status End As Status
								, A.DataUpdate
								,B.Nome As NomeEvento
								,B.SiglaEvento
								,B.CodEvento
								, case When  A.CodParticipante is not null Then  (
													SELECT 
																Count(X.CodVooTrecho) 
													FROM 
																NET_PARTICIPANTE_VOO  X
													Where 
																X.CodParticipante = A.CodParticipante And x.CodEvento = C.CodEvento) Else 0 End As Trechos
		FROM 
						NET_PARTICIPANTE A
						,NET_EVENTO B
						,NET_PARTICIPANTE_EVENTO C
WHERE     
					((A.Nome  Like  @pNome + '%' ) Or (@pNome IS NULL OR @pNome  = ''))
			And  ((A.Email   Like  @pEmail  + '%' ) Or (@pEmail  IS NULL OR @pEmail   = ''))
			And ((A.Status = @pStatus) Or (@pStatus IS NULL OR @pStatus  = 3))
			And (A.CodParticipante = C.CodParticipante)
			And (C.CodEvento  = B.CodEvento )
			And (C.CodEvento  =  @pCodEvento  OR (@pCodEvento IS NULL OR @pCodEvento = 0))

	SET NOCOUNT OFF 
	END 

