• This might at least help a bit, but it won't address the main problems of the query (user defined scalar function and function on columns in the where clause or others that we can't see).

    select @QTY_cost = (select round(sum(TXN.Fn_bacashcalculation(TXN.Transaction_Code,

    TXN.SEC_TYPE_CODE1,

    TXN.Sec_Type_Code2,

    TXN.TRADE_AMOUNT,

    'Y') +

    TXN.Fn_bacashcalculation(TXN.TRANSACTION_CODE,

    TXN.SEC_TYPE_CODE1,

    TXN.SEC_TYPE_CODE2,

    TXN.TRADE_AMOUNT,

    'N')), 0) as QTY

    from TXN.TRANSACTION_DETAILS TXN with (nolock) --Why NOLOCK? Are you fine with inconsistent results?

    where TXN.RUBEL_ID = @Rubel_ID

    and TXN.SECURITY_ID2 = @Security_ID

    and not (TXN.TRANSACTION_CODE in ('dp', 'wd')

    and TXN.SECURITY_ID1 = TXN.SECURITY_ID2

    )

    and TRADE_DATE <= @Report_dt

    and not (TXN.TRANSACTION_CODE in ('sa', 'pa')

    and isnull(SETTLE_DATE, TRADE_DATE) > @Report_dt

    )

    and TXN.END_DT is null

    );

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2