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

x
Solved

Function or SP

Posted on 2012-09-12
Medium Priority
416 Views
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
Question by:LCNW

LVL 75

Assisted Solution

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

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

ID: 38418875
Thanks for the suggestions.
0

Featured Post

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…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll