Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Usage of Functions in Stored Procedures


Usage of Functions in Stored Procedures

Author
Message
SQL Joker
SQL Joker
SSC Eights!
SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)

Group: General Forum Members
Points: 836 Visits: 76
Comments posted to this topic are about the item Usage of Functions in Stored Procedures
Anipaul
Anipaul
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6275 Visits: 1407
The article was nice. I hopoe the code is right. But when I tried to run the code I am getting error in the Insert statement.

BEGIN

INSERT INTO CaseDetail VALUES(200,300,1)

SET @count = @count -1

END


I am not having time to check the error but do we require to write the column names in the syntax. I may done some silly mistakes also.



Confucius247
Confucius247
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 40
The final select statement where you use a local variable is different to the pure select statement, it only works because you've got all the values exactly the same. So the examples you give are broken because you've been lazy and used exactly the same values.

If your data really is like that something's wrong with the data structure.

All this article shows is how you should never use functions unless you know exactly what they do, precisely the opposite of what you say at the beginning. In this case the function is so useless you might as well throw it away.

My attitude with functions is avoid like the plague, as they break the rule of thinking in sets while programming SQL.
SQL Joker
SQL Joker
SSC Eights!
SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)SSC Eights! (836 reputation)

Group: General Forum Members
Points: 836 Visits: 76
Hi Paul,

The following chunk of code worked fine for me.

CREATE TABLE CaseDetail (CaseId INT IDENTITY(1,1), QTY NUMERIC(14,4), weightedQty NUMERIC(14,4), ShipmentId INT)

--We assume that these cases belongs to Shipment with id 1

DECLARE @count INT
SET @count =15000

WHILE (@count >0)
BEGIN
INSERT INTO CaseDetail VALUES(200,300,1)
SET @count = @count -1
END

select count(caseid) from CaseDetail

Thank you and have a nice day,
Sreeju
SuperDBA-207096
SuperDBA-207096
UDP Broadcaster
UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)UDP Broadcaster (1.5K reputation)

Group: General Forum Members
Points: 1479 Visits: 711
There may be a few instances where functions can speed things along, I've found most of the time they hinder performance instead of helping it.

The times they've been useful to me is when I inherit something that's been designed poorly, and theres no $$$ to redesign it, but I have to make something work.
Anipaul
Anipaul
SSCertifiable
SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)SSCertifiable (6.3K reputation)

Group: General Forum Members
Points: 6275 Visits: 1407
Hi Sreeju

I may be wrong. May typed wrongly. I will check it again. Thnks for the info ......



Grant Fritchey
Grant Fritchey
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17605 Visits: 32267
I've been burned way too many times by poor use of functions to be any kind of advocate for them. However, if you really want to drill down on them, then I recommend Andy Novick's book. He covers appropriate use of functions very well.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
The Dixie Flatline
The Dixie Flatline
Hall of Fame
Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)Hall of Fame (3.4K reputation)

Group: General Forum Members
Points: 3398 Visits: 6899
My own rule of thumb is to avoid functions that contain queries. Yes, they make for easy reading, but it's far too likely that they will get and reused in SELECT queries. RBAR bad.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Matt Minnis
Matt Minnis
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 146
What about matrix type calculations?
How can they be accelerated?
What I mean is you have a bunch (about a dozen) of tables with charts of tax rates,freight rates, etc.
Each row will have different values from each table and then they get computed for the final result.
We have functions for various lookups and calculations, some will be repeated, some won't
How can you improve this type of process?
I can see your technique for the 1 function, but I'm not sure how it will scale in a large stored proc with lots of function calls and computations.
SQLZ
SQLZ
SSC Eights!
SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)

Group: General Forum Members
Points: 874 Visits: 940
I don't know.... I've run both your inline and variable procedures and on my machine they both take about 470ms, give or take a few ms. In fact, if anything I've found the variable method slightly slower by up to 50ms.

Did you dropcleanbuffers before running each procedure? If not then you might find that the cached results are interfering with your numbers.

Also, if you check out the io statistics, more logical reads are performed with the variable method.

I'm not sure I see the benefit in running the exact same query twice, as you're doing in the variable method. Unless I'm missing something, which is quite possible Wink

Karl
source control for SQL Server
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search