• Without more information, I would say you could do it like this.

    CREATE function [dbo].[myfunc]

    ( @a int, @b-2 int, @C money)

    returns table

    AS

    return

    select d = (isnull(sum(d.cola),0)+@c) * -1

    from (Select distinct c.cola

    from tab1 b

    join tab2 c

    on b.cola = c.colb

    where b.cola <= @b-2 and

    b.colb = @a) c

    join tab3 d

    on c.cola = d.colb and

    @a <> d.cola

    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