USE DAGAZ	
	IF OBJECT_ID('NET_INSERT_REGIONAL') IS NOT NULL
		DROP PROC NET_INSERT_REGIONAL

	GO
		CREATE PROC NET_INSERT_REGIONAL
	(
			  @pCodRegional INT = NULL
			,@pRegional VARCHAR(255) =  NULL 
			,@pStatus INT = NULL
			,@pCodUsuario INT = NULL
			,@pDataCadastro DATETIME = NULL
			,@PMessageErr VARCHAR(150) =  NULL OUTPUT
	)
	AS 
		
		BEGIN
			SET NOCOUNT ON
			
			IF @pCodRegional = 0 OR @pCodRegional IS NULL 
				BEGIN

	IF NOT EXISTS(SELECT CodRegional FROM NET_REGIONAL WHERE CodRegional= @pCodRegional )
		INSERT INTO NET_REGIONAL
							(      
								 Regional
								,Status
								,CodUsuario
								,DataCadastro
							)VALUES
							(
								  @pRegional
								,@pStatus
								,@pCodUsuario
								,@pDataCadastro
							)
					ELSE 
								SET @PMessageErr = 'Este regional  ja existe.Verifique!' ;
			END
		ELSE
	IF NOT EXISTS(SELECT CodRegional FROM NET_REGIONAL WHERE Regional= @pRegional  And CodRegional <> @pCodRegional )
						UPDATE    NET_REGIONAL
									SET               
											  Regional = @pRegional
											,Status = @pStatus
											,CodUsuario = @pCodUsuario
											,DataCadastro = @pDataCadastro
									WHERE     
														CodRegional = @pCodRegional
						ELSE
								SET @PMessageErr = 'Este regional  ja existe.Verifique!' ;


					SELECT @PMessageErr  As MessageReturn;
			SET NOCOUNT OFF 
	END 
