SQL XP 09
SQL XP 09
Objectives
In this lesson, you will learn to:
Create a stored procedure
Execute a stored procedure
Pass parameters to a stored procedure
Modify a stored procedure
Return values from a stored procedure
Return multiple values from a stored procedure
Call a procedure from another stored procedure
Recompile a stored procedure
Drop a stored procedure
Implementing Stored Procedures
Getting Started
A stored procedure is a collection or batch of T-SQL
statements and control-of-flow language that is stored under
one name, and executed as a single unit
Benefits of Stored Procedures
Improved performance
Reduction in network congestion
Better consistency
Better security mechanism
Implementing Stored Procedures
Just a Minute…
What are the benefits of using stored procedures?
Implementing Stored Procedures
Task List
Identify the object that can be used to solve the above
problem
Draft the stored procedure on paper
Create the stored procedure in the database
Check the existence of the procedure in the database
Execute the procedure
Implementing Stored Procedures
Just a Minute…
The query to obtain the list of candidates and their
recruitment agencies is:
SELECT 'Candidate Name' = vFirstName ,
'Recruitment Agency' = cName
FROM ExternalCandidate
JOIN RecruitmentAgencies
ON ExternalCAndidate.cAgencyCode =
RecruitmentAgencies.cAgencyCode
Create a stored procedure for the same.
Implementing Stored Procedures
Task List
Identify a method to write a generic procedure that will
generate results for variable inputs
Draft statements to create a procedure
Create the stored procedure in the database
Check the existence of the procedure in the database
Prepare test cases with existing and non-existing values
Execute the procedure with the existing value
Execute the procedure with the non-existing value
Implementing Stored Procedures
Task List
Identify a method to modify the existing procedure
Draft statements to modify a procedure
Verify that the procedure has been modified
Execute the procedure
Implementing Stored Procedures
Task List
Identify a method to write a procedure that will return a value
Draft the procedure
Modify the procedure in the database
Check that the procedure has been modified in the database
Execute the procedure
Implementing Stored Procedures
WHERE cCity=@city)
Implementing Stored Procedures
Task List
Identify a method to modify the existing procedure
Create the procedure in the database
Check the existence of the procedure in the database
Execute the procedure with parameters
Implementing Stored Procedures
Just a Minute…
You need to modify a stored procedure. Which command will
you use to modify the procedure?
Implementing Stored Procedures
Current Strength : 83
Task List
Identify a method to write a generic procedure that will return
more than one value
Create the procedure in the database
Check whether the procedure exists in the database
Execute the procedure with parameters
Implementing Stored Procedures
Summary
In this lesson, you learned that:
A stored procedure is a collection of various T-SQL statements
that are stored under one name and executed as a single unit
A stored procedure can be created either in the Enterprise
Manager or in the Query Analyzer window with the CREATE
PROCEDURE statement
A stored procedure allows you to declare parameters, variables,
and use T-SQL statements and programming logic
A stored procedure provides better performance, security, and
accuracy, and reduces network congestion
Implementing Stored Procedures
Summary (Contd.)
The various types of stored procedures are:
User-defined
System
Temporary
Remote
Extended
A stored procedure accepts and returns data through the
following:
Input parameters
Implementing Stored Procedures
Summary (Contd.)
Output parameters
Return statements
A stored procedure can be executed using the EXECUTE
statement
A stored procedure can be altered using the ALTER
PROCEDURE statement
A stored procedure can be viewed using the sp_help and the
sp_helptext system procedures
To generate an updated execution plan, you must recompile
the stored procedure each time you execute the stored
procedure
Implementing Stored Procedures
Summary (Contd.)
To recompile a stored procedure automatically, you must create
the stored procedure with a WITH RECOMPILE option
A stored procedure can be dropped using the DROP
PROCEDURE statement
Extended stored procedures are Dynamic-Link Libraries (DLLs)
that SQL Server can dynamically load and execute