0% found this document useful (0 votes)
19 views12 pages

Stored Procedures

Uploaded by

Rita Rawhy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views12 pages

Stored Procedures

Uploaded by

Rita Rawhy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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:

scalar functions: (returns only one value)


scalar functions return a single value based on the input parameters passed to the function. They are used to perform
calculations or operations on input values and return a single result.

Example of a scalar function in SQL Server:

CREATE FUNCTION dbo.AddNumbers


(
@num1 INT,
@num2 INT
)
RETURNS INT
AS
BEGIN
RETURN @num1 + @num2;
END;

You can then use this function in a query like this:

SELECT dbo.AddNumbers(5, 7) -- Returns 12


Functions
Table-Valued Functions:
-inline table function (return table)
-multistatmenet table function (return table with logic)
Table-valued functions return a table as a result. They can be used in the FROM clause of a SELECT statement, and they can
accept parameters.
Example of a table-valued function in SQL Server:

CREATE FUNCTION dbo.GetEmployeesByDepartment


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

You can use this function in a query like this:

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.

Stored procedures are typically stored in a database and can be invoked by


applications, triggers, or other stored procedures.

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:

-- Executing the stored procedure


EXEC GetEmployeeCount;
-- or
EXECUTE GetEmployeeCount;

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

You might also like