This article describes the differences between a stored procedure and a user defined functions in SQL Server. There are several differences beween a strored proc and a function and one of the key difference is, a function must return a value while in a stored procedure, its optional. Rest of the differences are explained below.
Stored Procedure
A stored procedure is prepared SQL code that you save so you can reuse the code over and over again. So if you think about a query that you write over and over again, instead of having to write that query each time you would save it as a stored procedure and then just call the stored procedure to execute the SQL code that you saved as part of the stored procedure. Stored procedures are also a way to reduce burden on the app and execute SQL query on the server side.
In addition to running the same SQL code over and over again you also have the ability to pass parameters to a stored procedure, so depending on what the need is, the Stored Procedure can act accordingly based on the parameter values that were passed.
Stored Procedures can also improve performance. Many tasks are implemented as a series of SQL statements. Conditional logic applied to the results of the first SQL statements determine which subsequent SQL statements are executed. If these SQL statements and conditional logic are written into a Stored Procedure, they become part of a single execution plan on the server. The results do not need to be returned to the client to have the conditional logic applied; all of the work is done on the server.
Benefits of Stored Procedures
- Precompiled execution - SQL Server compiles each Stored Procedure once and then reutilizes the execution plan. This results in tremendous performance boosts when Stored Procedures are called repeatedly.
- Reduced client/server traffic - If network bandwidth is a concern in your environment then you'll be happy to learn that Stored Procedures can reduce long SQL queries to a single line that is transmitted over the wire.
- Efficient reuse of code and programming abstraction - Stored Procedures can be used by multiple users and client programs. If you utilize them in a planned manner then you'll find the development cycle requires less time.
- Enhanced security controls - You can grant users permission to execute a Stored Procedure independently of underlying table permissions
User Defined Functions
Like functions in programming languages, SQL Server User Defined Functions are routines that accept parameters, perform an action such as a complex calculation, and returns the result of that action as a value. The return value can either be a single scalar value or a result set.
Functions in programming languages are subroutines used to encapsulate frequently performed logic. Any code that must perform the logic incorporated in a function can call the function rather than having to repeat all of the function logic.
SQL Server supports two types of functions
- Built-in functions - Operate as defined in the Transact-SQL Reference and cannot be modified. The functions can be referenced only in Transact-SQL statements using the syntax defined in the Transact-SQL Reference.
- User Defined Functions - Allow you to define your own Transact-SQL functions using the CREATE FUNCTION statement. User Defined Functions use zero or more input parameters, and return a single value. Some User Defined Functions return a single, scalar data value, such as an int, char, or decimal value.

Henthiramoorthy InpanathanPosted Dec 29, 2022, 8:02 AM
Nice Article
Puja KumariPosted Dec 6, 2018, 1:13 AM
In some sites I have read that Procedures complies only one time and called multiple times without compiling. But on the other hand function is compile every time when it is called. Can you explain.
nithin rajPosted Oct 22, 2018, 10:28 AM
Nice article,too much easy to learn
Sasi RekhaPosted May 10, 2018, 6:26 AM
First i used to prefer C# corner always for any information .Excellent article about storedprocedure
HemlataPosted Jun 20, 2017, 2:03 AM
Nice Explanation..Thanks!
sanchit jindalPosted Jul 21, 2016, 3:57 PM
Too good...
Rajan SinghPosted Oct 30, 2013, 8:54 AM
Very good explanation.