Solved

Can I have a conditional statement in an Inline UDF

Posted on 2004-09-02
6
302 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

688 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