IF OBJECT_ID('dbo.fn_fco_nro_trab_arp') IS NOT NULL
    DROP FUNCTION dbo.fn_fco_nro_trab_arp 
GO
CREATE FUNCTION dbo.fn_fco_nro_trab_arp
(    
    @pch_compania char(4),    
    @psi_anio smallint,    
    @pti_mes tinyint,    
    @pch_sucursal char(10)    
)    
RETURNS decimal(5,0)    
AS    
BEGIN    
DECLARE     
    @vdc_total decimal(5,0),    
    @vdt_fec_ini smalldatetime,    
    @vdt_fec_fin smalldatetime,    
    @vti_por_suc tinyint    
        
    IF IsNull(RTrim(LTrim(@pch_sucursal)), '') = ''     
        SET @vti_por_suc = 0    
    ELSE    
        SET @vti_por_suc = 1    
    
    SET @vdt_fec_ini = Convert(smalldatetime, RTrim(@psi_anio) + Right('00' + RTrim(@pti_mes), 2) + '01')  
    SET @vdt_fec_fin = DateAdd(d, -1, DateAdd(m, 1, @vdt_fec_ini))  
  
    
    SELECT    
        @vdc_total = count(*)     
    FROM    
        trabajadores_grales     
    WHERE    
        compania = @pch_compania And    
        clase_nomina <> 'NP' And    
        fecha_ingreso <= @vdt_fec_fin And    
        (fecha_baja is null or fecha_baja >= @vdt_fec_ini)     
    
    RETURN @vdc_total       
END    

GO
GRANT EXECUTE ON dbo.fn_fco_nro_trab_arp TO PUBLIC
GO
