0% found this document useful (0 votes)
23 views20 pages

Advanced Databases 4th Lectuer Stored Proceduer

The document provides an overview of stored procedures in databases, detailing their definition, advantages, and types. Stored procedures are precompiled blocks of Transact-SQL code that enhance security, reduce network traffic, and allow for code reuse. It also explains how to create, update, and delete stored procedures, along with examples and parameter types.

Uploaded by

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

Advanced Databases 4th Lectuer Stored Proceduer

The document provides an overview of stored procedures in databases, detailing their definition, advantages, and types. Stored procedures are precompiled blocks of Transact-SQL code that enhance security, reduce network traffic, and allow for code reuse. It also explains how to create, update, and delete stored procedures, along with examples and parameter types.

Uploaded by

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

Advanced Database

‘Stored Procedures‘
Lecturer: Sirwan A. Aula
[email protected]
Stored Procedures

A stored procedure is a group of Transact-SQL statements


that act as a single block of code that performs a specific
task. This block of code is identified by an assigned name
and is stored in the database in a compiled form.
Stored procedures are useful for performing repetitive tasks.
This eliminates the need for repetitively typing out multiple
Transact-SQL statements and then repetitively compiling
them.
Stored procedures support user-declared variables,
conditional execution and other programming features.
Also, parameters can be passed between the stored
procedure and the calling program.
Advantages – 1

 Database developers or administrators write stored procedures to


perform a variety of tasks related to database access and
management.
 Using stored procedures offer numerous advantages over using
Transact-SQL statements. These are:
 Improved Security

 Precompiled Execution

 Reduced Client/Server Traffic

 Reuse of Code
Advantages – 2

 Improved Security: by associating database privileges with stored


procedures. Users can be given permission to execute a stored
procedure even if the user does not have permission to access the
tables or views.
 Precompiled Execution: stored procedures are compiled during the
first execution. For every subsequent execution, SQL Server reuses this
precompiled version. This reduces the time and resources required for
compilation.
Advantages – 3

 Reduced Client/Server Traffic: stored procedures help in reducing


network traffic.
 Reuse of Code: stored procedures can be used multiple times. This
eliminates the need to repetitively type out hundreds of Transact-SQL
statements every time a similar task is to be performed.
Stored Procedure vs. SQL Statement
SQL Statement Stored Procedure
Creating
- Check syntax
- Compile
First Time First Time
- Check syntax - Execute
- Compile - Return data
- Execute
- Return data
Second Time Second Time
- Check syntax - Execute
- Compile - Return data
- Execute
- Return data
Types of Stored Procedures

 System Stored Procedures


 Extended Stored Procedures
 Temporary Stored Procedures
System Stored Procedures

 System stored procedures are used in database administrative and


informational activities. These procedures provide easy access to the
metadata information about database objects such as system tables,
user-defined tables, views, and indexes.
 System stored procedures logically appear in the sys schema of system
and user-defined databases. When referencing a system stored
procedure, the sys schema identifier is used. The system stored
procedures are stored physically in the Resource database and have
the ‘sp_’ prefix.
Extended Stored Procedures

The extended stored procedures are not residents of SQL


Server. They are procedures that are implemented as
dynamic-link libraries (DLL) executed outside the SQL Server
environment.
Extended stored procedures use the ‘xp_’ prefix. Tasks that
are complicated or cannot be executed using Transact-SQL
statements are performed using extended stored
procedures.
Temporary Stored Procedures

Stored procedures created for temporary use within a


session are called temporary stored procedures. These
procedures are stored in the tempdb database.
The tempdb system database is a global resource available
to all users connected to an instance of SQL Server. It holds
all temporary tables and temporary stored procedures.
PARAMETERS

• Parameters are the means to pass values to and from the


calling environment to the server.
• These are the values that will be processed or returned via
the execution of the procedure.
• There are three types of parameters:
• IN, OUT, and IN OUT.
• Modes specify whether the parameter passed is read in or a
receptacle for what comes out.

Bordoloi and
Bock
Types of Parameters

Bordoloi and
Bock
Create, update and delete a
procedure
Create a Procedure
Update a Procedure
Delete a Procedure
Create a Procedure

Syntax
Example 1 (Without parameters)
Example 2 (With parameters)
Example 3 (Using RETURN)
Syntax

CREATE PROC[EDURE] procedure_name


[ @parameter_name data_type] [= default] OUTPUT][,...,n]
AS
T-SQL_statement(s)
Example 1 (Without parameters)

CREATE PROC Departments_Members


AS
SELECT Dep_Name, COUNT(Emp_ID) NumberOfMember
FROM Departments D, Employees E
WHERE D.Dep_ID = E.Dep_ID
GROUP BY Dep_Name
Run Procedure
Execute Departments_Members
Example 2 (With parameters)

CREATE PROC Department_Members @DeptName varchar(50)


AS
SELECT Dep_Name, COUNT(Emp_ID) NumberOfMember
FROM Departments D, Employees E
WHERE D.Dep_ID = E.Dep_ID and Dep_Name = @DeptName
GROUP BY Dep_Name
Run Procedure
Execute Department_Members ‘Computer’
Example 3 (Using RETURN )

CREATE PROC GROUPLEADER_MEMBERS


@Emp_Code varchar(10) = null
AS
IF @Emp_Code is null
BEGIN
PRINT 'Please enter Employee Code!'
RETURN
END
SELECT * FROM Employees
WHERE EMP_EMP_ID = (SELECT EMP_ID FROM Employees
WHERE Emp_Code = @Emp_Code)
ORDER BY Emp_Name
Update a Procedure

ALTER PROC[EDURE] procedure_name


[ @parameter_name data_type]
[= default] [OUTPUT]
[,...,n]
AS
t-sql_statement(s)
Delete a Procedure

 DROP PROCEDURE procedure_name

You might also like