Top Banner
STORE PROCEDURES
40
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Store procedures

STORE PROCEDURES

Page 2: Store procedures

FARZAN WADOOD (BSCS-12-F-23)ASAD IQBAL (BSCS-12-F-02)WAJEEH ASLAM (BSCS-12-F-03)SYED UZAIR ALI (BSCS-12-F-36)

Page 3: Store procedures

OBJECTIVES

• Learn about the features and benefits of stored procedures.

• Create useful stored procedures.

• Understand input and output parameters.

• Learn how to validate input and handle errors in stored procedures.

• Use the Transact-SQL Debugger to debug stored procedures.

• Use temp tables in your stored procedures.

• Understand the uses for triggers and learn how to write one.

• Use an INSTEAD OF trigger with a view.

Page 4: Store procedures

WAJEEH ASLAM

Page 5: Store procedures

INTRODUCTION

Page 6: Store procedures

Stored Procedure

What Is a Stored Procedure?

Stored Procedure vs. SQL Statement

Create, update and delete a procedure

Page 7: Store procedures

What Is a Stored Procedure?

A stored procedure is a group of sql statements that has been created

and stored in the database.

A stored procedure is a collection of T-SQL(Transact SQL) statements that

SQL Server compiles into a single execution plan.

Stored procedure will accepts input parameters so that a single

procedure can be used over network by several clients using different

input data.

Page 8: Store procedures

What Is a Stored Procedure?

Procedure is stored in cache area of memory when the stored procedure

is first executed so that it can be used repeatedly. SQL Server does not

have to recompile it every time the stored procedure is run.

It can accept input parameters, return output values as parameters, or

return success or failure status messages.

Page 9: Store procedures

Advantages

Greater security as store procedures are always stored on the database

server

SQL can be optimized by the DBMS compiler

Code sharing resulting in:

Less work

Standardized processing

Specialization among developers

Page 10: Store procedures

Advantages

Stored procedure will reduce network traffic and increase the

performance.

If we modify stored procedure all clients will get the updated

stored procedure.

Reusability: do not need to write the code again and again

Page 11: Store procedures

Disadvantages

Using stored procedures can increase the amount of server processing.

Business Logic in SP: Do not put all of your business logic into stored

procedures.

Maintenance and the agility (control)of your application becomes an

issue when you must modify business logic in T-SQL.

Page 12: Store procedures

Why Stored Procedures Are Faster?

The command is parsed for syntax. Any commands that are

syntactically incorrect are rejected.

The command is then translated into an internal format known as a

sequence tree or query tree.

The command is optimized based on estimated performance costs,

and an execution plan is generated from the sequence tree that

contains all the necessary steps to check constraints and carry out

the command.

The command is executed.

Page 13: Store procedures

Uses For Stored Procedures

Stored procedures reduce the complexity of client coding.

Reduced utilization of network bandwidth.

Stored procedures can optimize data access by returning limited result

sets.

Errors can be handled at the server, in the stored procedure, and not at

the client.

A stored procedure is written once, and can be accessed from many

client applications.

Page 14: Store procedures

SYED UZAIR ALI

Page 15: Store procedures

Building Stored Procedures

A stored procedure is a batch of Transact-SQL (T-SQL) code that is

saved internally in SQL Server.

Stored procedures can be used to mask complex SQL tasks from

clients,

improve the performance of your application,

enforce data and business rules, increase multi-user concurrency,

and reduce locking and blocking conflicts.

Page 16: Store procedures

Stored Procedure vs. SQL Statement

Second Time - Check syntax

- Compile

- Execute

- Return data

Second Time- Check syntax- Compile- Execute- Return data

Creating- Check syntax- Compile

First Time- Execute- Return data

Second Time- Execute- Return data

STORED PROCEDURE

SQL STATEMENT

Page 17: Store procedures

Types of Stored Procedure

System Stored Procedure

User Defined Stored Procedure

Non Parameterized Stored Procedure

Parameterized Stored Procedure

Page 18: Store procedures

System Stored Procedure

System Stored Procedures are useful in performing administrative and

informational activities in SQL Server

System procedures can be executed from any database, without

You can change the context of the system procedures, since they can be used

in any data based at a base context

System Procedure can return zero or n values

Page 19: Store procedures

User Defined Stored Procedure

These Stored Procedures are defined by the user in SQL Server. To create a

Stored Procedure use the Create procedure statement.

Function must return a value.

Page 20: Store procedures

FARZAN WADOOD

Page 21: Store procedures

Create, update and delete a procedure

Create a Procedure

Update a Procedure

Delete a Procedure

Page 22: Store procedures

CREATE A PROCEDURE

Page 23: Store procedures

Create a Procedure

Syntax

Example 1 (Without parameters)

Example 2 (With parameters)

Page 24: Store procedures

Syntax

Page 25: Store procedures

Example 1 (Without parameters)

Page 26: Store procedures

Example 2 (With parameters)

Page 27: Store procedures

UPDATE A PROCEDURE

Page 28: Store procedures

Update a Procedure

Page 29: Store procedures

DROP A PROCEDURE

Page 30: Store procedures

Drop a Procedure

Page 31: Store procedures

ASAD IQBAL

Page 32: Store procedures

VARIABLES IN STORED PROCEDURES

Page 33: Store procedures

Variables in Stored Procedures

Variables can be used in stored procedures the same way that you

use in other languages

in any programming language. SQL Server requires that you

explicitly declare

each variable with its corresponding data type by using the

DECLARE

statement. To declare multiple variables, separate the declarations

by commas:

Page 34: Store procedures

Syntax

For uninitialized variables

Page 35: Store procedures

Cont.…..

For initialize variables

All uninitialized variables have a default value of NULL

To assign a value, use the SELECT statement.

Page 36: Store procedures

Syntax

For initialized variables

Page 37: Store procedures

Error Handling in Stored Procedures

A great new option that was added in SQL Server 2005 was the ability to

use the Try..Catch paradigm that exists in other development languages. 

Doing error handling in SQL Server has not always been the easiest thing,

so this option definitely makes it much easier to code for and handle errors.

Page 38: Store procedures

Syntax

Page 39: Store procedures

Summary

Stored procedures are a very powerful database component

System-stored procedures are useful for database administration and

maintenance

User-defined stored procedures are useful for whatever you have

designed them for.

They have advantages over views and queries in that they are precompiled

After their first execution, their execution plan is stored in the procedure cache that resides in random access memory (RAM).

Another benefit of stored procedures is that you can assign permission to a user to run a stored procedure even if that user does not have permissions on the underlying tables.

Page 40: Store procedures