Solved

Can I have a conditional statement in an Inline UDF

Posted on 2004-09-02
6
300 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 59
GeoClustering  and AOG 25 41
SQL Consolidate rows 3 24
Returning Message/Value Back to Access from a SQL Server trigger 3 30
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…
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

685 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