Menu Close

Difference between Stored Procedure and Function in SQL Server

In this article we will learn about Difference between Stored Procedure and Function in SQL Server. Both stored procedures and functions are database objects which contain a set of SQL statements to complete a task. In many ways, both are different from each other. In this article, we’re going to discuss the differences between stored procedures and functions. Please read my previous article Different Types of SQL Keys: Example and Uses.

Stored Procedures

Stored Procedures are pre-compiled objects which are compiled for the first time and its compiled format is saved, which executes (compiled code) whenever it is called.

Syntax

CREATE [ OR ALTER ] { PROC | PROCEDURE }
    [schema_name.] procedure_name [ ; number ]
    [ { @parameter [ type_schema_name. ] data_type }
        [ VARYING ] [ = default ] [ OUT | OUTPUT | [READONLY]
    ] [ ,...n ]
[ WITH <procedure_option> [ ,...n ] ]
[ FOR REPLICATION ]
AS { [ BEGIN ] sql_statement [;] [ ...n ] [ END ] }
[;]

<procedure_option> ::=
    [ ENCRYPTION ]
    [ RECOMPILE ]
    [ EXECUTE AS Clause ]

Functions

A function is compiled and executed every time whenever it is called. A function must return a value and cannot modify the data received as parameters. For more about functions, please refer to the article Different types of Functions.

Function may contain a set of statement as stored procedure but generally we create function if there is some calculations which we can do frequently.

Syntax

CREATE [ OR ALTER ] FUNCTION [ schema_name. ] function_name
( [ { @parameter_name [ AS ][ type_schema_name. ] parameter_data_type
 [ = default ] [ READONLY ] }
    [ ,...n ]
  ]
)
RETURNS return_data_type
    [ WITH <function_option> [ ,...n ] ]
    [ AS ]
    BEGIN
        function_body
        RETURN scalar_expression
    END
[ ; ]

Basic Differences between Stored Procedure and Function in SQL Server

  • The function must return a value but in Stored Procedure it is optional. Even a procedure can return zero or n values.
  • Functions can have only input parameters for it whereas Procedures can have input or output parameters.
  • Functions can be called from Procedure whereas Procedures cannot be called from a Function.

Advance Differences between Stored Procedure and Function in SQL Server

  • The procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.
  • Procedures cannot be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.
  • Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.
  • Functions that return tables can be treated as another row set. This can be used in JOINs with other tables.
  • Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset operations.
  • An exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a Function.
  • We can use Transactions in Procedure whereas we can’t use Transactions in Function.

Additional Differences between Stored Procedure and Function in SQL Server

  1. Stored procedure may or may not return a value that value is only integer type, but function return the values of any data type.
  2. We can use out parameter in stored procedure but not with function.
  3. Stored procedure is compile once when we created but function is always recompile whenever we call it.
  4. Within a Stored procedure we can write such statement which can affect the database or can be time dependent(for example DML statement).But such statement can not return within function.
  5. Stored procedure can not be called within the function but a function is called within a Stored procedure.
  6. Exception handling can be done in Stored procedure but not in function.
  7. Within a Stored procedure we can write such statement which will display data directly to the user. But In function we can not write such SELECT Statement.

Conclusion

So far we discussed Difference between SP and Function -The function must return a value but in SP it is optional.

1 Comment

Leave a Reply

Your email address will not be published.