Solved

Can I have a conditional statement in an Inline UDF

Posted on 2004-09-02
6
294 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:JAMES
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:bukko
ID: 11962507
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
0
 
LVL 8

Expert Comment

by:bukko
ID: 11962545
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'
)
0
 

Author Comment

by:JAMES
ID: 11962563
Neat Idea - I will give it a try and report back.

Thanks.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 

Author Comment

by:JAMES
ID: 11962722
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.
0
 
LVL 8

Accepted Solution

by:
bukko earned 500 total points
ID: 11964158
Odd - what values are you passing in to the @Style parameter? (If it isn't obvious)

You shouldn't need LIKE unless the values don't match exactly.
Try:

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

Shouldn't make any difference, but worth a go.

bukko
0
 

Author Comment

by:JAMES
ID: 11964229
It wouldnt even let me save the function with "=" I had to specify "LIKE".

Its working, thats the important bit ;-)
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how the fundamental information of how to create a table.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now