Você está na página 1de 10

STORED PROCEDURES

In This Presentation..

Introduction to Variables Stored Procedures Types of Stored Procedures System Stored Procedures Extended Stored Procedures User-defined Stored Procedures Creating Stored Procedures Executing Stored Procedures System Stored Procedures Managing Stored Procedures

Introduction to Variables

Variables are defined memory locations that are given a specific name and contain some value. Variables are mainly used for calculations or to store some value for stored procedures. The name of the local variable must be prefixed with @ sign. declare keyword is used to declare a variable. set keyword is used to assign a vale to a variable. SQL Server uses several system global variables that hold information useful to the database user.

Stored Procedures

Stored Procedures are the database objects that encapsulate data manipulation and retrieval statements. Stored Procedures contain a collection of precomplied T-SQL statements . Stored Procedures are processed as a single unit. Stored Procedures are mainly used for reducing the network traffic in a Client - Server environment. Stored Procedures are also referred to as SPROCS

Types of Stored Procedures


Stored Procedures are classified into THREE Types. System Stored Procedures: Supplied by SQL Server and are precompiled version of T-SQL statements. Used to report and manage information in the system. They begin with sp_. User-defined Stored Procedures: We can create our own Stored Procedures, compile them execute them as per our need. Extended Stored Procedures: These SPs work outside SQL Server and are stored as DLLs and are executed dynamically. They begin with xp_

Creating Stored Procedures


CREATE PROC[EDURE] procedure-name
[WITH ENCRYPTION] as BEGIN <Transact - SQL statement>, [<Transact - SQL statement>,] [<Transact - SQL statement>,] . . [<Transact - SQL statement>] END

Executing Stored Procedures

We can execute a complied stored procedure with the use of EXEC or EXECUTE command
EXEC <procedure-name> or EXECUTE <procedure-name>

However, if the execution is the first statement of the batch, then there is no need to include the EXEC or EXECUTE command.

Modifying Stored Procedures

Stored Procedures can be modified using the ALTER statement similar to other DB Objects.
ALTER PROC[EDURE] <procedure-name> <new schema>

Stored Procedures can be dropped using DROP statement similar to other DB Objects.
DROP PROC[EDURE] <procedure-name>

System Stored Procedures


Sp_help
Sp_helpdb Sp_helptext

Sp_databases
Sp_columns sp_depends

Sp_pkeys
Sp_indexes,

etc

Managing Stored Procedures


Stored

Procedures can be recompiled whenever required using sp_recompile system Stored Procedure. Definition can be extracted using sp_helptext We can encrypt the definition of the Stored Procedure as well and secure it from unauthorized users.

Você também pode gostar