Stored Procedures
Stored Procedures
FUNCTIONS
>
Rita Rawhy
Yassin Yasser
Functions
A User-Defined Function (UDF) in a database is a custom-created function that
allows users to define their own functions to perform specific tasks within the
database. These functions are written in a specific programming language
supported by the database management system (DBMS) and can encapsulate a
series of SQL statements or other programming logic.
The SQL Server user-defined functions help you simplify your development by
encapsulating complex business logic and make them available for reuse in every
.query
Functions
User Defined Function Consist Of
1. Signature (Name, Parameters, ReturnType)
2. Body
(Body Of Function Must be Select Statement Or Insert Based On Select)
May Take Parameters Or Not
Any SQL Server Function must return value
Functions
User Defined Function Consist Of
1. Signature (Name, Parameters, ReturnType)
2. Body
(Body Of Function Must be Select Statement Or Insert Based On Select)
May Take Parameters Or Not
Any SQL Server Function must return value
Functions
There are two main types of user-defined functions in databases:
SELECT *
;FROM dbo.GetEmployeesByDepartment(1)
Stored Procedures
A stored procedure is a precompiled collection of one or more SQL statements or
procedural statements (e.G., Control-of-flow statements like IF, WHILE) that are
stored and can be executed on demand.
when you call a stored procedure for the first time , sql server creates an execution
plan and stores it in the cache memory , then in the second time it reuses the plan
stored in memory to execute the sp very fast and with reliable performance
Stored Procedures
Benefits Of SP:
1. Performance
2. Security (Hide Meta Data)
3. Network Wise
4. Hide Business Rules
5. Handle Errors (SP Contain DML)
6. Accept Input And Out Parameters => Make It More Flexible
Creating a stored procedure --
CREATE PROCEDURE GetEmployeeCount
AS
BEGIN
SELECT COUNT(*) AS EmployeeCount
FROM Employees;
END;
> The CREATE PROCEDURE statement is used to define a new stored procedure named
GetEmployeeCount.
>The body of the stored procedure is enclosed within the AS keyword.
>The stored procedure contains a single SQL statement that selects the count of records from
".the "Employees" table and aliases it as "EmployeeCount
Once the stored procedure is created, it can be executed using the EXEC or EXECUTE
keyword:
This will execute the stored procedure, and the result (the count of employees) will be
.returned
Stored procedures can also take parameters. Here's an example:
-- Creating a stored procedure with parameters
CREATE PROCEDURE GetEmployeeByDepartment
@DepartmentID INT
AS
BEGIN
SELECT *
FROM Employees
WHERE DepartmentID = @DepartmentID;
END;
In this example, the stored procedure GetEmployeeByDepartment takes a parameter
@DepartmentID, and it selects all employees from the "Employees" table where the
department ID matches the provided parameter.
-- Executing the stored procedure with a parameter
;EXEC GetEmployeeByDepartment @DepartmentID = 1
Difference Between
Functions & Stored Procedures
Functions Procedures
.A function has a return type and returns a value A procedure does not have a return type. But it returns
.values using the OUT parameters
The compilation of a function occurs when we call them in Needs to be occur once , and we do not have to compile
the program every time them every time
You cannot use a function with Data Manipulation queries. You can use DML queries such as insert, update, select
.Only Select queries are allowed in functions . etc… with procedures
A function does not allow output parameters .A procedure allows both input and output parameters
.You cannot manage transactions inside a function .You can manage transactions inside a procedure
You cannot call stored procedures from a function .You can call a function from a stored procedure
.You can call a function using a select statement .You cannot call a procedure using select statements