IF OBJECT_ID('dbo.fntb_fco_dian_1002') IS NOT NULL
    DROP FUNCTION dbo.fntb_fco_dian_1002
GO
CREATE FUNCTION dbo.fntb_fco_dian_1002
(
    @pch_compania char(4),
    @psi_anio smallint
)
-- JCL FCO 25-ABR-07
-- FORMATO 1002 PARA LA DIAN
RETURNS TABLE
AS
RETURN
(
/* Cuantías Mayores */
SELECT
    1 As secuencia, 
    registros =  
    RTrim(concepto) + ',' + tdoc + ',' + cedula + ',,' + -- DV
    ape1 + ',' + ape2 + ',' + nom1 + ',' + nom2 + ',,' + -- Razón Social
    dire + ',' + dpto + ',' + mpio + ',' + RTrim(Floor(base)) + ',' + RTrim(Floor(valor))
FROM vw_fco_dian_x_ced a
WHERE a.compania = @pch_compania
  AND a.anio = @psi_anio
  AND a.formato = 1002
  AND a.concepto = 2301
)
GO
GRANT SELECT ON dbo.fntb_fco_dian_1002 TO PUBLIC
GO
