Everything we can accomplish using functions can be done by stored procedures itself,
This is false.
- Stored procedures cannot be recomposed or inlined (like how a
VIEW
or Table-Valued Function can - and SQL Server 2017 can now inline scalar functions too).
- Stored procedures cannot be proven "safe" by the database engine (as stored procedures can perform DML/DDL operations, whereas UDFs can only perform read-only operations).
- Stored procedures cannot be used to perform operations on scalar values.
You can make the argument that everything you can do inside a query that would use a UDF could be done without that UDF - and this is technically true (because stored procedures are Turing Complete), but it's like saying we don't need for
, while
, try/catch
, using
, or async
in C# because surely if
and goto
is good enough for everyone!
Code-quality, maintainability, and runtime performance matters - appropriate use of UDFs results in not only aesthetically beautiful queries, but also better performance thanks to inlining, recomposition, expression tree pruning, and so on.
Besides, who is using stored procedures today when all the cool kids are using Entity Framework? :D
Footnote
I want to elaborate on my remark:
Stored procedures cannot be used to perform operations on scalar values.
While it's true that stored procedures have return values and output parameters - they still aren't classed the same as true UDFs because a stored procedure return value can only be an int
value (they're closer to program exit codes in POSIX/Win32 than a real return value) and stored procedure output parameters cannot be used inside another query unless you're doing ugly hacks with CURSOR
or Dynamic SQL (Injection).