Chapter 02 - 02 Stored Procedures
Chapter 02 - 02 Stored Procedures
(PART 02)
STORED PROCEDURES
AND FUNCTIONS
WAAM Wanniarachchi
References
• SQL :The Complete Reference (Second Edition)
JAMES R. GROFF AND PAUL N. WEINBERG
Stored Procedures
• A stored procedure is a collection of T-SQL statements that
SQL Server compiles into a single execution plan
• Stored procedures can be used in a situation where same
query is used over and over again.
• This queries can be saved as a stored procedure and call it.
• With stored procedures, several capabilities normally
associated with programming languages are grafted onto the
SQL language.
• Collectively, the structures that implement these capabilities
form a stored procedure language (SPL).
• Conditional execution
An IF…THEN…ELSE structure allows a SQL procedure to test a
condition and carry out different operations depending on the
result.
• Looping
A WHILE or FOR loop or similar structure allows a
sequence of SQL operations to be performed
repeatedly, until some terminating condition is met.
• Block structure
A sequence of SQL statements can be grouped into a
single block and used in other flow-of-control constructs
as if the statement block were a single statement.
• Named variables
A SQL procedure may store a value that it has calculated,
retrieved from the database, or derived in some other way
into a program variable, and later retrieve the stored
value for use in subsequent calculations.
• Much of the original enthusiasm for stored procedures was
because of their performance impact in a client/server
database architecture
• Without stored procedures, every SQL operation requested
by an application program (running on the client computer
system) would be sent across the network to the database
server, and would wait for a reply message to be returned
across the network.
• If a logical transaction required six SQL operations, six
network round trips were required.
• With stored procedures, the sequence of six SQL operations
could be programmed into a procedure and stored in the
database.
• In this way, six network round trips could be cut to one round
trip—the request and reply for executing the stored
procedure.
Normal Database
• REUSABILITY
• CAN BE CALLED ANYTIME THAT IT NEEDS TO BE EXECUTED
• MAINTAINABILITY
• BECAUSE SCRIPTS ARE IN ONE LOCATION, UPDATES AND TRACKING OF
DEPENDENCIES BASED ON SCHEMA CHANGES BECOMES EASIER
• TESTING
CAN BE TESTED INDEPENDENT OF THE APPLICATION
• ISOLATION OF BUSINESS RULES
HAVING STORED PROCEDURES IN ONE LOCATION MEANS THAT THERE'S NO
CONFUSION OF HAVING BUSINESS RULES SPREAD OVER POTENTIALLY DISPARATE CODE
FILES IN THE APPLICATION
• SECURITY
LIMIT DIRECT ACCESS TO TABLES VIA DEFINED ROLES IN THE DATABASE
Create Procedure
CREATE PROCEDURE
Execute Procedure
Example:
1. Create Stored procedure GET_PARA_EMP with two parameters Gender and
Department ID ,
• Example:
Note:
When naming stored procedures, Microsoft recommends not to use sp_ as prefix
as aloo the system stored procedures are prefix with sp_
Update Stored procedure
Drop Stored procedure
Execute Functions
User Defined Functions (UDF)
Ex:
Write a function to calculate the Profit of the each branch.
From Table Profit
END