Store procedures

40
STORE PROCEDURES

Transcript of Store procedures

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)

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.

WAJEEH ASLAM

INTRODUCTION

Stored Procedure

What Is a Stored Procedure?

Stored Procedure vs. SQL Statement

Create, update and delete a procedure

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.

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.

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

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

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.

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.

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.

SYED UZAIR ALI

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.

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

Types of Stored Procedure

System Stored Procedure

User Defined Stored Procedure

Non Parameterized Stored Procedure

Parameterized Stored Procedure

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

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.

FARZAN WADOOD

Create, update and delete a procedure

Create a Procedure

Update a Procedure

Delete a Procedure

CREATE A PROCEDURE

Create a Procedure

Syntax

Example 1 (Without parameters)

Example 2 (With parameters)

Syntax

Example 1 (Without parameters)

Example 2 (With parameters)

UPDATE A PROCEDURE

Update a Procedure

DROP A PROCEDURE

Drop a Procedure

ASAD IQBAL

VARIABLES IN STORED 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:

Syntax

For uninitialized variables

Cont.…..

For initialize variables

All uninitialized variables have a default value of NULL

To assign a value, use the SELECT statement.

Syntax

For initialized variables

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.

Syntax

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.