0% found this document useful (0 votes)
25 views16 pages

Phase 3 Create Stored Procedures

The document discusses stored procedures in SQL databases. It explains that stored procedures allow SQL statements to be packaged and reused, improving maintainability and optimized database access. The document covers how to create stored procedures, including those with input and output parameters. It also discusses modifying, displaying, renaming, and deleting stored procedures. Finally, it introduces conditional logic concepts like IF/ELSE statements and CASE expressions that can be used within stored procedures.
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)
25 views16 pages

Phase 3 Create Stored Procedures

The document discusses stored procedures in SQL databases. It explains that stored procedures allow SQL statements to be packaged and reused, improving maintainability and optimized database access. The document covers how to create stored procedures, including those with input and output parameters. It also discusses modifying, displaying, renaming, and deleting stored procedures. Finally, it introduces conditional logic concepts like IF/ELSE statements and CASE expressions that can be used within stored procedures.
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/ 16

Transitioning into Namibian University of Science and Technology

PHASE 3

Using Stored Procedures

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Revision (Database Fundamentals)


Important you know how to use them to understand
this Phase
• Manipulating Database Data
– Inserting data
– Updating data
– Deleting data

• Querying Database
– Retrieve data

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Objectives
After completing this lesson, you should be able to understand the
following:

• Create stored procedures


• Display the definitions of stored procedures
• Rename stored procedures
• Delete stored procedures
• Conditional Logic
• Exception Handling
• Trigger

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Introduction
A stored procedure is a collection of SQL statements that allows you to
perform a task repeatedly.
You can create the procedure once and reuse it any number of times in
your program.

This improves:
• maintainability of your application
• allow applications to access the database in a uniform and optimized
manner

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Create Stored Procedures


Can have parameters that are used for input and output.
Can be called from client programs or other stored procedures
How it works
Use the CREATE PROCEDURE to create stored procedures
The AS keyword separates the signature (the procedure’s name and
parameter list, but here you define no parameters) of the stored
procedure from its body (the SQL that makes up the procedure).
Create procedure sp_Select_ALL_PersonContact
As
select Contact.Title, Contact.FirstName, Contact.LastName
from Person.Contact
Execute sp_Select_ALL_PersonContact
NOTE: prefix sp_ (coded in SQL), prefix xp_ isnt written in SQL.
Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Create a Stored Procedure with an Input


Parameter
Create a stored procedure that produces a list of contacts for a given title of
any person. Pass the title to the to the stored procedure for use in a query.
Create procedure sp_Contact_By_Title
@Title nvarchar(8)
As
select Contact.Title, Contact.FirstName, Contact.LastName
from Person.Contact
where Contact.Title = @Title
Execute sp_Contact_By_Title ‘Mr’

NOTE: Only the parameter name and datatype, so by default it is an input


parameter. Parameter names start with @.
Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Create a Stored Procedure with an Output


Parameter
Output parameters are usually used to pass between stored procedures.
They are used to return a value like a function in a programming language.
Create procedure sp_CountContacts_By_Title
@Title nvarchar(8), @TitleCount int = 0 output
As
select Contact.Title, Contact.FirstName, Contact.LastName
from Person.Contact
where Contact.Title = @Title
select @TitleCount = count(*)
from Person.Contact
where Title = @Title
return @TitleCount
Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Test a Stored Procedure with an Output


Parameter
Declare @return_value int,
@TitleCount int

Execute @return_value = sp_CountContacts_By_Title


@Title = ‘Mr’,
@TitleCount = @TitleCount output

Select ‘Total Title Count’ = @return_value

Tip: input parameters can also be assigned default values.

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Modifying Stored Procedures

Use the Alter Procedure procedure_name statement to modify an existing


stored procedure.
Alter procedure sp_Select_All_PersonContact
As
select Contact.Title, Contact.FirstName, Contact.LastName
from Person.Contact
Order by Contact.LastName

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Displaying the Definition of Stored Procedures


SQL Server offers a way to view the definition of the objects created in the
database. This is known as metadata retrieval.
Execute sp_helptext ‘sp_Select_All_Employees’
sp_helptext ‘sp_Select_All_PersonContact

The statement sp_helptext is a predefined SQL Server stored procedure that


accepts an object name as a parameter and shows the definition of the
object passed to the sp_helptext as the parameter.

Note: sp_helptext doesn’t work with table objects, you can’t see the
definition of the CREATE TABLE statement used while creating the table
object.

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Renaming Stored Procedures


SQL Server allows you to rename the objects.
sp_rename ‘sp_Select_All_PersonContact,
‘sp_Select_All_ContactDetails’

The statement sp_rename is a predefined SQL Server stored procedure that


accepts an object’s old name and the object ‘s new name as parameters.

Note: sp_rename works very well with most of the objects when you want
to rename them, such as tables, columns, and other objects.

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Deleting Stored Procedures


Once a stored procedure is created, you can delete it if you don’t need its
functionality.
Drop procedure sp_Select_All_ContactDetails

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Conditional Logic
You can use IF and ELSE commands to You can use CASE expression to
control the flow of commands. compare the results of an expression
IF <some condition> with a series of tests and return a result
when the test returns true.
then <some command>
ELSE <some condition>
SELECT JobTitle,
then <some command>
CASE Gender
You can use loops to process multiple
records. WHEN ‘M’ THEN ‘Male’
DECLARE @cnt INT = 0; WHEN ‘F’ THEN ‘Female’
WHILE @cnt <cnt_total ELSE ‘Unknown Value’
BEGIN {… statements …} END
SET @cnt = @cnt +1; END
Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Exception Handling
When user-defined or system-related exceptions are encountered , the
control is shifted to the Exception Handling section.

Note: you can create your own error messages.

• User-defined (errors created by the user)

• System-related (errors triggered by the system)

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Triggers
A trigger defines an action the Database should take.

Triggers are executed by the database when specific DML operations (insert,
update, deletes) commands are performed on specific tables.

You must have a CREATE TRIGGER system privilege.

To alter a trigger’s body, the trigger must be re-created or replaced.

Explanation: ATM Machine scenario

Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming
Transitioning into Namibian University of Science and Technology

Summary
In this lesson, you should have learned:

• To create stored procedures


• Develop and understand of what’s involved in creating, executing,
and modifying a stored procedure from SSMS
• To create appropriate command parameters for the stored procedure
parameter
• Understand the usage of conditional statements, exception handling
and triggers

Note: In the next phase, you will learn how to use SQL queries to produce
XML output.
Agarwal, V. V. (2012). Beginning C# 5.0 databases (2nd ed.) & Celko, J. (2011). SQL for Smarties: Advanced SQL Programming

You might also like