XUDF SQLServer

XUDF SQLServer

-- **************************************************************************
-- gart_unidefs_get_canbase
-- DEISTER WebStudio XSQL-UDFUNC  -  Engine: sqlserver
-- **************************************************************************
CREATE FUNCTION gart_unidefs_get_canbase (
    @p_codart char(16), 
    @p_varlog char(6), 
    @p_udmdoc char(6), 
    @p_udmalt char(6), 
    @p_candoc decimal(12,3), 
    @p_canalt decimal(12,3)
)
    RETURNS  decimal(12,3)
AS
BEGIN
    DECLARE @m_udmbas char(6)
    DECLARE @m_udmaux char(6)
    DECLARE @m_udmrel char(1)
    DECLARE @m_candes decimal(12,3)

    -- =================================
    -- Function body
    -- =================================
    SET @m_candes = NULL
    SELECT udmbas, udmaux
           INTO m_udmbas, m_udmaux
           FROM garticul
          WHERE codigo  = p_codart
        
    IF m_udmbas IS NULL BEGIN
      RAISE EXCEPTION -746, 0, 'Artículo: [' || p_codart || '] no encontrado en garticul';
      END
    
    IF p_udmdoc = m_udmbas BEGIN
      RETURN p_candoc
      END
    
    IF p_udmalt IS NOT NULL AND p_udmalt = m_udmbas  BEGIN
      RETURN p_canalt
      END
    
        IF 
                m_udmrel = 'B' OR (m_udmrel = 'A' AND p_udmalt IS NULL)
             BEGIN
            END
    
    IF m_candes IS NULL AND p_udmalt IS NOT NULL BEGIN
            IF m_udmrel = 'B' BEGIN
                END
      ELSE BEGIN
              
        END
      
      END
    
    RETURN m_candes
    -- =================================
    -- SQLSERVER needs RETURN at last instruction (error:455)
    -- =================================
      RETURN p_candoc

END