Link to home
Start Free TrialLog in
Avatar of JAMES
JAMES

asked on

Can I have a conditional statement in an Inline UDF

I have the following inline UDF :-


ALTER FUNCTION dbo.OptionPremiums (@PriceMove float = 0, @Style text)
RETURNS TABLE
AS

      RETURN (      SELECT      dbo.Options.Contract, dbo.Options.Currency, dbo.Options.Exchange,
                  dbo.Options.Style, dbo.Options.StrikePrice, dbo.Options.ExpiryDate,
                  dbo.Options.PC, dbo.Options.UnderlyingPrice, dbo.Options.Volatility,
                  dbo.Options.InterestRate, dbo.Options.ValueDate, dbo.Options.DeltaConvention,
            (dbo.RealTimePrices.Price * 1000) + @PriceMove AS Price,
            dbo.EuropeanPremium('20040823', dbo.Options.StrikePrice, dbo.Options.ExpiryDate, dbo.Options.PC,
            (dbo.RealTimePrices.Price * 1000) + @PriceMove, 0.25, 0.03, '', '', 365) AS Premium
FROM        dbo.Options CROSS JOIN dbo.RealTimePrices)


....but I would like to return a different TABLE depending on a the value of @Style (ie execute a different statement).  I have tried the usual IF statement as would be used in a scaler function but it doesnt seem to like it.

Any thoughts?

Thanks.

James.
Avatar of bukko
bukko

I don't think so.
Unless you incorporate both tables into the query, such as with UNION, then remove the unrequired table using a WHERE clause which will always return false.

bukko
e.g.:...

ALTER FUNCTION dbo.OptionPremiums (@PriceMove float = 0, @Style text)
RETURNS TABLE
AS

     RETURN (      SELECT     dbo.Options.Contract, dbo.Options.Currency, dbo.Options.Exchange,
               dbo.Options.Style, dbo.Options.StrikePrice, dbo.Options.ExpiryDate,
               dbo.Options.PC, dbo.Options.UnderlyingPrice, dbo.Options.Volatility,
               dbo.Options.InterestRate, dbo.Options.ValueDate, dbo.Options.DeltaConvention,
            (dbo.RealTimePrices.Price * 1000) + @PriceMove AS Price,
            dbo.EuropeanPremium('20040823', dbo.Options.StrikePrice, dbo.Options.ExpiryDate, dbo.Options.PC,
            (dbo.RealTimePrices.Price * 1000) + @PriceMove, 0.25, 0.03, '', '', 365) AS Premium
FROM        dbo.Options CROSS JOIN dbo.RealTimePrices
WHERE       @Style = 'Option1'

UNION ALL

SELECT     dbo.Options2.Contract, dbo.Options2.Currency, dbo.Options2.Exchange,
               dbo.Options2.Style, dbo.Options2.StrikePrice, dbo.Options2.ExpiryDate,
               dbo.Options2.PC, dbo.Options2.UnderlyingPrice, dbo.Options2.Volatility,
               dbo.Options2.InterestRate, dbo.Options2.ValueDate, dbo.Options2.DeltaConvention,
            (dbo.RealTimePrices2.Price * 1000) + @PriceMove AS Price,
            dbo.EuropeanPremium('20040823', dbo.Options2.StrikePrice, dbo.Options2.ExpiryDate, dbo.Options2.PC,
            (dbo.RealTimePrices2.Price * 1000) + @PriceMove, 0.25, 0.03, '', '', 365) AS Premium
FROM        dbo.Options2 CROSS JOIN dbo.RealTimePrices2
WHERE       @Style = 'Option2'
)
Avatar of JAMES

ASKER

Neat Idea - I will give it a try and report back.

Thanks.
Avatar of JAMES

ASKER

Works like a dream except I had to alter the lines

WHERE       @Style = 'Option1'
WHERE       @Style = 'Option2'

to

WHERE       @Style LIKE 'Option2'
WHERE       @Style LIKE 'Option1'

Does that seem the correct thing to do?

Thanks.

James.
ASKER CERTIFIED SOLUTION
Avatar of bukko
bukko

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JAMES

ASKER

It wouldnt even let me save the function with "=" I had to specify "LIKE".

Its working, thats the important bit ;-)