?
Solved

Can I have a conditional statement in an Inline UDF

Posted on 2004-09-02
6
Medium Priority
?
304 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

777 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