You are on page 1of 23

CHAPTER 02

(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

Applications using Stored


Procedures
Advantages

• 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

1. Call by the name


[NAME] > EXECUTE
Create pro: with parameters
• The real power of stored procedures is the ability to pass parameters and have the
stored procedure handle the differing requests that are made.

Example:
1. Create Stored procedure GET_PARA_EMP with two parameters Gender and
Department ID ,
• Example:

2. Executethe Stored procedure to retrieve male employees who


works in department 3
3. What is the output you receive?
4. Identify the reason for interruption.
5. RE execute the Stored procedure GET_PARA_EMP
• Example:

2. What happens if parameter given as

Order of the value is important


• Giving parameter names will eliminate the issue
View Script of the stored procedure

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

Encryption of the Stored Procedure


IF Else
• Declaring a variable
(Ex)
Declare @No int
select @No=MAX(Employee.Salary) from Employee
print @No;
(Ex)
select @No=Sum(Employee.Salary) from Employee
print @No;
(Ex)
select @No=MAX(Employee.Salary) from Employee
print @No;
If Else Cont
(Ex)
1. Change the gender to Boys if the current gender
is Male
If Else Cont
(Ex)
1. Check the availability of records in the tables. Check
the employee Name and Gender from the table and
and insert the records
User Defined Functions (UDF)
Scaler Functions
Scaler Functions always return a single value.
Crate Functions

Execute Functions
User Defined Functions (UDF)

Ex:
Write a function to calculate the Profit of the each branch.
From Table Profit
END

You might also like