SQL Server - Scalar UDFs vs Inline
Scalar UDFs are good if you're working with a handful of rows or a variable. However, when you throw a lot of rows at them, they generally slow down your queries.Microsoft recommends inlining them whenever possible. Microsoft Website . Let's see if they are right. Let's say we have to Proper Case a set of rows, just because where we live, if you don't Proper Case, you're an outcast. Step 1 - Create table CREATE TABLE dbo . ProperCasing ( IDCol int not null identity primary key , StringCol varchar ( 500 ) ) Step 2 - Add a ton of rows INSERT INTO dbo . ProperCasing ( StringCol ) SELECT ( 'This object has the name ' + OBJECT_NAME ( Object_Id )) FROM sys . Objects GO 1000 -- Scalar UDF to ProperCase a string that works in 2016, 2017, etc -- You can use STRING_AGG in 2017+ and use STUFF in 2016 Step 3 - Create Scalar UDF to ProperCase a string -- You can use STRING_AGG in 2017+ and STUFF in ...