Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

CT004-3.

5-3
Advanced Database Systems
Implementing Stored
Procedures
Topic & Structure of Lesson

• Implementing Stored Procedures


• Creating Parameterized Stored Procedures

Module Code & Module Title Slide Title SLIDE 2


Learning Outcomes

By the end of this lesson you should


be able to:
• Implement stored procedures.
• Create parameterized stored procedures.

Module Code & Module Title Slide Title SLIDE 3


What Is a Stored Procedure?

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


compiled into a single execution plan.

• Stored Procedures Can:


– Contain statements that perform operations
– Accept input parameters
– Return status value to indicate success or failure
– Return multiple output parameters

Module Code & Module Title Slide Title SLIDE 4


Advantages of Using Stored Procedures

• Share Application Logic


– All clients can use the same stored procedures to ensure consistent data access and
modification.
• Shield Database Schema Details
– Users never need to access the tables directly
• Provide Security Mechanisms
– Users can be granted permission to execute a stored procedure even if they do not have
permission to access the tables or views to which the stored procedure refers.

Module Code & Module Title Slide Title SLIDE 5


Advantages of Using Stored Procedures

• Improve Performance
– Stored procedures implement many tasks as a series of Transact-SQL statements. All
these Transact-SQL statements become part of a single execution plan on the server.
• Reduce Network Traffic
– Rather than sending hundreds of Transact-SQL statements over the network, users can
perform a complex operation by sending a single statement, which reduces the number
of requests that pass between client and server

Module Code & Module Title Slide Title SLIDE 6


Syntax for Creating Stored Procedures

• Create in current database by using the CREATE


PROCEDURE statement

CREATE
CREATE {{ PROC PROC || PROCEDURE
PROCEDURE }} [schema_name.]
[schema_name.]
procedure_name
procedure_name
[[ {{ @parameter
@parameter data_type
data_type }}
[[ VARYING
VARYING ]] [[ == default
default ]] [[ [[ OUT
OUT [[ PUT
PUT ]]
]] [[ ,...n
,...n ]]

AS
AS
{{ <sql_statement>
<sql_statement> [;]
[;] [[ ...n
...n ]}
]}
• Use EXECUTE to run stored procedure

EXECUTE
EXECUTE [schema_name.]
[schema_name.] procedure_name
procedure_name

Module Code & Module Title Slide Title SLIDE 7


Example for Creating Stored Procedures
• Create in current database by using the CREATE
PROCEDURE statement

CREATE
CREATE PROCEDURE
PROCEDURE Production.LongLeadProducts
Production.LongLeadProducts
AS
AS
SELECT
SELECT Name,
Name, ProductNumber
ProductNumber
FROM
FROM Production.Product
Production.Product
WHERE
WHERE DaysToManufacture
DaysToManufacture >=
>= 11

•GO
GOUse EXECUTE to run stored procedure

EXECUTE
EXECUTE Production.LongLeadProducts
Production.LongLeadProducts

Module Code & Module Title Slide Title SLIDE 8


Guidelines for Creating Stored Procedures

ü Qualify object names inside procedure

ü Create one stored procedure for one task

ü Create, test, and troubleshoot

ü Avoid sp_ prefix in stored procedure names

Module Code & Module Title Slide Title SLIDE 9


Syntax for Altering and Dropping Stored
Procedures
• ALTER PROCEDURE
ALTER
ALTER PROC
PROC Production.LongLeadProducts
Production.LongLeadProducts
AS
AS
SELECT
SELECT Name,
Name, ProductNumber,
ProductNumber, DaysToManufacture
DaysToManufacture
FROM
FROM Production.Product
Production.Product
WHERE
WHERE DaysToManufacture
DaysToManufacture >=
>= 11
ORDER
ORDER BY
BY DaysToManufacture
DaysToManufacture DESC,
DESC, Name
Name

GO
GO
• DROP PROCEDURE

DROP
DROP PROC
PROC Production.LongLeadProducts
Production.LongLeadProducts

Module Code & Module Title Slide Title SLIDE 10


Creating Parameterized Stored
Procedures

• Input Parameters
• Output Parameters and Return Values

Module Code & Module Title Slide Title SLIDE 11


Creating Parameterized Stored
Procedures

Parameterized stored procedures contain 3 major components:

Input parameters

Output parameters

Return values

Module Code & Module Title Slide Title SLIDE 12


Input Parameters
• Provide appropriate default values
• Validate incoming parameter values, including null checks
ALTER
ALTER PROC
PROC Production.LongLeadProducts
Production.LongLeadProducts
@MinimumLength
@MinimumLength int
int == 11 --
-- default
default value
value
AS
AS

IF
IF (@MinimumLength
(@MinimumLength << 0)
0) ---- validate
validate
BEGIN
BEGIN
RAISERROR('Invalid
RAISERROR('Invalid lead
lead time.',
time.', 14,
14, 1)
1)
RETURN
RETURN
END
END

SELECT
SELECT Name,
Name, ProductNumber,
ProductNumber, DaysToManufacture
DaysToManufacture
FROM
FROM Production.Product
Production.Product
WHERE
WHERE DaysToManufacture
DaysToManufacture >=
>= @MinimumLength
@MinimumLength
ORDER
ORDER BY
BY DaysToManufacture
DaysToManufacture DESC,
DESC, Name
Name

EXEC
EXEC Production.LongLeadProducts
Production.LongLeadProducts @MinimumLength=4
@MinimumLength=4

Module Code & Module Title Slide Title SLIDE 13


Output Parameters and Return Values

CREATE
CREATE PROC
PROC HumanResources.AddDepartment
HumanResources.AddDepartment
@Name
@Name nvarchar(50),
nvarchar(50), @GroupName
@GroupName nvarchar(50),
nvarchar(50),
@DeptID smallint OUTPUT
@DeptID smallint OUTPUT
AS
AS
IF
IF ((@Name
((@Name == '')
'') OR
OR (@GroupName
(@GroupName == ''))
''))
RETURN
RETURN -1
-1

INSERT
INSERT INTO
INTO HumanResources.Department
HumanResources.Department (Name,
(Name, GroupName)
GroupName)
VALUES
VALUES (@Name,
(@Name, @GroupName)
@GroupName)

SET
SET @DeptID
@DeptID == SCOPE_IDENTITY()
SCOPE_IDENTITY()
RETURN 0
RETURN 0

DECLARE
DECLARE @dept
@dept int,
int @result
int
int, @result int
int
EXEC
EXEC @result
@result == AddDepartment
AddDepartment 'Refunds','Refunds',
'Refunds',
AddDepartment
AddDepartment '', @dept '',
'', @dept
'Refunds', @dept
@dept OUTPUT
OUTPUT
'',
OUTPUT OUTPUT
IF
IF (@result
(@result == 0)
0)
SELECT
SELECT
SELECT
SELECT @deptID
@dept
@deptID
@dept
ELSE
ELSE
SELECT
SELECT 'Error
'Error during
during insert'
insert'

Module Code & Module Title Slide Title SLIDE 14


Summary of Main Teaching Points

• Stored procedures definition.


• How to create and execute a simple stored procedures.
• How to create and execute a parameterized stored
procedures.
• How to modify and remove existing stored procedures.

Module Code & Module Title Slide Title SLIDE 15


Question and Answer Session

Q&A
Module Code & Module Title Slide Title SLIDE 16
Next Lesson

• Implementing Triggers

Module Code & Module Title Slide Title SLIDE 17

You might also like