[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Function or SP

Posted on 2012-09-12
3
Medium Priority
?
416 Views
Last Modified: 2012-09-20
I have this BMI calculation running in a query and was wondering if it would be better to use a function or SP or ? and if yes, how to go about doing that. I don't feel like this is the most efficient way to do it but I had to get something working in a pinch.

MAX(CASE WHEN p.HeightUnits = 'Cm' AND
                      p.WeightUnits = 'Kgs' THEN ROUND(CAST(p.weight AS float) / square(CAST(p.height AS float) / 100), 1) WHEN p.HeightUnits = 'In' AND
                      p.WeightUnits = 'Lbs' THEN ROUND(CAST(p.weight AS float) / square(CAST(p.height AS float)) * 703, 1) WHEN p.HeightUnits = 'FtIn' AND
                      p.WeightUnits = 'Lbs' THEN ROUND(CAST(p.weight AS float) / square(CAST(p.height AS float)) * 703, 1) WHEN p.HeightUnits = 'Cm' AND
                      p.WeightUnits = 'Lbs' THEN ROUND((CAST(p.weight AS float) * 0.453592) / square(CAST(p.height AS float) / 100), 1) WHEN p.HeightUnits = 'In' AND
                      p.WeightUnits = 'Kgs' THEN ROUND(CAST(p.weight AS float) / square(CAST(p.height AS float) * 0.0254), 1) WHEN p.HeightUnits = 'FtIn' AND
                      p.WeightUnits = 'Kgs' THEN ROUND(CAST(p.weight AS float) / square(CAST(p.height AS float) * 0.0254), 1) END) AS BMI
0
Comment
Question by:LCNW
3 Comments
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 total points
ID: 38392886
it totally depends on your need, functions (except the inline functions )are slower in performance, buy they can be called inside a select query.
While sps  are faster,  you cannot use them in a select query, but if moving everything inside an sp is possible, then i would go with sp. otherwise you need to decide, whether to go for  a function or a select statement with the above case statements
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1600 total points
ID: 38393022
From a purely performance/efficiency standpoint, inline code will be much better than either a function or a stored proc.

Although, for readability reasons, I prefer this formatting :-) :


MAX(CASE
    WHEN p.HeightUnits = 'Cm' AND  p.WeightUnits = 'Kgs' THEN
        ROUND(CAST(p.weight AS float) / SQUARE(CAST(p.height AS float) / 100), 1)
    WHEN p.HeightUnits = 'In' AND p.WeightUnits = 'Lbs' THEN
        ROUND(CAST(p.weight AS float) / SQUARE(CAST(p.height AS float)) * 703, 1)
    WHEN p.HeightUnits = 'FtIn' AND p.WeightUnits = 'Lbs' THEN
        ROUND(CAST(p.weight AS float) / SQUARE(CAST(p.height AS float)) * 703, 1)
    WHEN p.HeightUnits = 'Cm' AND p.WeightUnits = 'Lbs' THEN
        ROUND((CAST(p.weight AS float) * 0.453592) / SQUARE(CAST(p.height AS float) / 100), 1)
    WHEN p.HeightUnits = 'In' AND p.WeightUnits = 'Kgs' THEN
        ROUND(CAST(p.weight AS float) / SQUARE(CAST(p.height AS float) * 0.0254), 1)
    WHEN p.HeightUnits = 'FtIn' AND p.WeightUnits = 'Kgs' THEN
        ROUND(CAST(p.weight AS float) / SQUARE(CAST(p.height AS float) * 0.0254), 1)
    END) AS BMI
0
 
LVL 1

Author Closing Comment

by:LCNW
ID: 38418875
Thanks for the suggestions.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

831 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