Phase 3 Create Stored Procedures
Phase 3 Create Stored Procedures
PHASE 3
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
• 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:
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
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
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
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
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
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.
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.
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:
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