how to optimize this function with union all

0

I use all those tables depending on the u_lgs_type this function I use in a procedure

ALTER FUNCTION [dbo].[SBO_FN_LGS_OBT_OBTENER_tipo_cambio](@Codes int)  
RETURNS varchar(250)   
AS   
-- Returns the stock level for the product.  
BEGIN  
    DECLARE @resp numeric(4,3);
    DECLARE @aa int;  

     SELECT @resp =b.moneda,
     @codes=(case when a.StornoToTr is null then a.TransId
                when a.StornoToTr <> null then a.StornoToTr
                else a.TransId end) 

    from OJDT A INNER JOIN 
     (
    select transid,U_LGS_TIPO,DocRate as moneda,U_LGS_TIPO as tipo from oinv where TransId=@Codes union all
    select transid,U_LGS_TIPO,DocRate as moneda,U_LGS_TIPO from opch where TransId=@Codes union all
    select transid,U_LGS_TPCE,DocRate as moneda,U_LGS_TPCE from ovpm where TransId=@Codes union all 
    select transid,U_LGS_TPCE,DocRate as moneda,U_LGS_TPCE from ORCT where TransId=@Codes union all
    select transid,U_LGS_TIPO,DocRate as moneda,U_LGS_TIPO from ODLN where TransId=@Codes  union all
    select transid,U_LGS_TIPO,DocRate as moneda,U_LGS_TIPO from orin where TransId=@Codes  union all
    select transid,U_LGS_TPCE,DocRate as moneda ,U_LGS_TPCE from orct where TransId=@Codes  union all
    select transid,U_LGS_TIPO,DocRate as moneda,U_LGS_TIPO from owtr where TransId=@Codes 

    ) b 
     ON A.TRANSID=B.TRANSID  and a.U_LGS_TIPO=b.tipo
        where a.transid= @codes
    RETURN @resp;  
END; 
    
asked by Danilo 24.08.2018 в 16:19
source

0 answers