SQL : Store Procedures Versus Functions

SQL : Store Procedures Versus Functions

SQL stored procedures and functions are versatile tools in SQL that allow users to develop reusable and optimized code for handling intricate database tasks. They allow users to bundle a set of SQL statements into a single, executable unit that can be reused with different inputs. This capability makes them essential for managing complex databases. By mastering SQL stored procedures and functions, users can write efficient, reusable code that not only enhances security but also simplifies maintenance and boosts database performance.

Stored Procedure

Creating stored procedures in SQL involves specifying a name, optionally defining parameters, and writing the SQL code to be executed.

Stored procedures can be executed with parameters that allow for flexible and dynamic execution. It can be used as modular programming. In other words, stored procedures are created once, stored, and called as many times as needed.

Stored procedures are stored in parsed and compiled state in the database.

Example --

-- Case 1: With no parameters
CREATE PROCEDURE spGetCustomers
AS
BEGIN
   SELECT * FROM customers
END

-- Case 2: With 2 parameters 
CREATE PROCEDURE spTranscAmount
 @source_account nvarchar(50),
 @dest_account nvarchar(50),
 @amount decimal(18,2)
AS
BEGIN
 BEGIN TRANSACTION
 UPDATE accounts SET balance = balance - @amount WHERE account_number = @source_account
 UPDATE accounts SET balance = balance + @amount WHERE account_number = @dest_account
 COMMIT TRANSACTION
END

Functions

Functions in SQL queries can enhance performance, minimize code duplication, and simplify complex processes. A function is compiled and executed every time it is called.

Example --

CREATE FUNCTION dbo.GetTotalSales(@StartDate DATE, @EndDate DATE)
RETURNS MONEY
AS
BEGIN
    DECLARE @TotalSales MONEY;
    SELECT @TotalSales = SUM(OrderTotal)
    FROM Orders
    WHERE OrderDate BETWEEN @StartDate AND @EndDate;

    RETURN @TotalSales;
END

Differences between Store Proc and Functions

Store ProceduresFunctions
1. No need to pass parameters.1. Must have at least one parameter.
2. Can return zero, single or multiple values.2. Always return single value.
3. Can perform any operation on database.3. DML statements not allowed like insert, update. Only Select allowed.
4. Can call a function from a stored procedure.4. Cannot call a stored procedure from a function.
5. Both table variables and temporary tables can be used5. Temporary tables cannot be used within a function. Only table variables can be used.
6. Stored procedures cannot be called from a Select/Where or Having statements. Execute statement has to be used to execute a stored procedure.6. Functions can be called from a Select statement.
7. Allows use of Try…Catch blocks for exception handling.7. Does not allow the use of Try…Catch blocks for exception handling.
8. Can have transactions within a stored procedure.8. Cannot have transactions within a function.
9. It runs as a unit, allowing the program to access data frequently without loading it into memory.9. Function runs concurrently in the computer’s memory

Thanks !!