SQL Stored Procedures and Functions

what-is-stored-procedure

Structured Query Language (SQL) is a powerful language used to communicate with relational database management systems (RDBMS).

Two essential components of SQL are stored procedures and functions, which are sets of precompiled SQL statements that perform specific tasks.

In this article, we’ll dive deep into stored procedures and functions, explaining each topic in detail with relevant examples and scenarios.

Stored Procedures

A stored procedure is a precompiled set of SQL statements stored in the database. It can be called by other SQL statements, client applications, or even other stored procedures. Stored procedures offer several advantages:

🔸 Code reusability

🔸 Enhanced security

🔸 Centralized business logic

🔸 Better performance

Creating a Stored Procedure

To create a stored procedure, use the CREATE PROCEDURE statement followed by the procedure name, parameters, and the SQL statements.

Example:

CREATE PROCEDURE spGetEmployeesByDepartment
  @DepartmentID INT
AS
BEGIN
  SELECT * FROM Employees WHERE DepartmentID = @DepartmentID
END;

Executing a Stored Procedure

To execute a stored procedure, use the EXEC (or EXECUTE) keyword followed by the procedure name and parameter values.

Example:

EXEC spGetEmployeesByDepartment 4;

SQL Functions

SQL functions are similar to stored procedures but return a single value or a table. Functions can be called within a SELECT statement, unlike stored procedures.

There are two types of functions in SQL:

🔸 Scalar functions: Return a single value

🔸 Table-valued functions: Return a table

Scalar Functions

Scalar functions return a single value based on the input parameters.

To create a scalar function, use the CREATE FUNCTION statement followed by the function name, parameters, the RETURNS keyword with the data type, and the SQL statements.

Example:

CREATE FUNCTION fnGetEmployeeSalary
  (@EmployeeID INT)
RETURNS DECIMAL(10, 2)
AS
BEGIN
  DECLARE @Salary DECIMAL(10, 2)
  SELECT @Salary = Salary FROM Employees WHERE EmployeeID = @EmployeeID
  RETURN @Salary
END;

Table-valued Functions

Table-valued functions return a table based on the input parameters.

To create a table-valued function, use the CREATE FUNCTION statement followed by the function name, parameters, the RETURNS keyword with the TABLE, and the SQL statements.

Example:

CREATE FUNCTION fnGetEmployeesByDepartment
  (@DepartmentID INT)
RETURNS TABLE
AS
RETURN (
  SELECT * FROM Employees WHERE DepartmentID = @DepartmentID
);

Calling Functions

To call a scalar function, simply use it in a SELECT statement.

Example:

SELECT dbo.fnGetEmployeeSalary(1) AS EmployeeSalary;

To call a table-valued function, treat it like a table in a SELECT statement.

Example:

SELECT * FROM dbo.fnGetEmployeesByDepartment(4) AS EmployeesByDepartment;

Stored procedures and functions are powerful SQL tools that enable you to centralize and reuse business logic, improve security, and enhance performance. Understanding their differences, syntax, and usage


Thank you for reading our blog, we hope you found the information provided helpful and informative. We invite you to follow and share this blog with your colleagues and friends if you found it useful.

Share your thoughts and ideas in the comments below. To get in touch with us, please send an email to dataspaceconsulting@gmail.com or contactus@dataspacein.com.

You can also visit our website – DataspaceAI

Leave a Reply