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

	GO
		CREATE PROC NET_SEL_CLIENTE
	(
			   	@pCodCliente	int
			   ,@pNomeFantasia	varchar(100)= null
			   ,@pRazaoSocial	varchar(100)= null
			   ,@pEmail	varchar(150)= null
			  ,@pSigla	char(20)	= NULL
			  ,@pCodEstado	int= NULL
			 ,@pCNPJ	varchar(30)= null
			 ,@pCodDepartamento  INT = null
			  ,@pStatus INT = NULL
			  ,@pOrder VARCHAR(50) =  NULL 
	)
	AS 
/*****************************************************
Author: Fabio Eduardo
Date:	 06/08/2008
Purpose: realizar uma busca de clientes


*****************************************************/
		BEGIN
			SET NOCOUNT ON
		
					SELECT 
								CodCliente
								,CodEstado
								,NomeFantasia
								,RazaoSocial
								,Site
								,Sigla
								,Endereco
								,Numero
								,Complemento
								,Bairro
								,Cidade
								,CEP
								,CNPJ
								,IE
								,CCM
								,Telefone
								,Celular
								,Fax
								,Email
								,INSC_E
								,CodDepartamento 
								,Status
								,ds_Cliente
								,Case When CodEstado>0 Then (SELECT TOP 1 Estado FROM NET_ESTADOS  Where NET_ESTADOS.CodEstado = NET_CLIENTE.CodEstado) Else '' End As Estado


		FROM 
						NET_CLIENTE
WHERE     
					((CodCliente= @pCodCliente) Or (@pCodCliente IS NULL OR @pCodCliente  = 0))
			And  ((NomeFantasia  Like  @pNomeFantasia + '%' ) Or (@pNomeFantasia IS NULL OR @pNomeFantasia  = ''))
			And  ((RazaoSocial  Like  @pRazaoSocial + '%' ) Or (@pRazaoSocial IS NULL OR @pRazaoSocial  = ''))
			And  ((Email  Like  @pEmail + '%' ) Or (@pEmail IS NULL OR @pEmail  = ''))
			And  ((Sigla  =@pSigla ) Or (@pSigla IS NULL OR @pSigla  = ''))
			And ((CodDepartamento = @pCodDepartamento ) Or (@pCodDepartamento  IS NULL OR @pCodDepartamento  = 0))
			And ((CodEstado = @pCodEstado) Or (@pCodEstado IS NULL OR @pCodEstado  = 0))
			And ((Status = @pStatus) Or (@pStatus IS NULL OR @pStatus  = 3))
	
ORDER BY 
					Sigla , RazaoSocial , NomeFantasia 




	SET NOCOUNT OFF 
	END 

