Stored procedure

From Wikipedia, the free encyclopedia

A stored procedure is a subroutine available to applications accessing a relational database system. Stored procedures (sometimes called a sproc or SP) are actually stored in the database.

Typical uses for stored procedures include data validation (integrated into the database) or access control mechanisms. Furthermore, stored procedures are used to consolidate and centralize logic that was originally implemented in applications. Large or complex processing that might require the execution of several SQL statements is moved into stored procedures and all applications call the procedures only.

Stored procedures are similar to user-defined functions (UDFs). The major difference is that UDFs can be used like any other expression within SQL statements, whereas stored procedures must be invoked using the CALL statement

 CALL procedure(...)

Stored procedures can return result sets, i.e. the results of a SELECT statement. Such result sets can be processed using cursors by other stored procedures by associating a result set locator, or by applications. Stored procedures may also contain declared variables for processing data and cursors that allow it to loop through multiple rows in a table. The standard Structured Query Language provides IF, WHILE, LOOP, REPEAT, and CASE statements, and more. Stored procedures can receive variables, return results or modify variables and return them, depending on how and where the variable is declared.

Contents

[edit] Implementation

The exact and correct implementation of stored procedure varies from one database system to another. Most major database vendors support them in some form. Depending on the database system, stored procedures can be implemented in a variety of programming languages, for example SQL, Java, or C and C++. Stored procedures written in non-SQL programming languages may or may not execute SQL statements themselves.

The increasing adoption of stored procedures led to the introduction of procedural elements to the SQL language in the SQL:1999 and SQL:2003 standards in the part SQL/PSM. That made SQL an imperative programming language. Most database systems offer proprietary and vendor-specific extensions, exceeding SQL/PSM. For example, Microsoft SQL Server allows for stored procedures to be written using Transact-SQL; Oracle calls its dialect PL/SQL, DB2 uses SQL/PL , PostgreSQL provides PL/pgSQL and also allows users to define their own function languages such as pl/perl or pl/php, and MySQL supports their own stored procedures that try to adhere closely to the SQL:2003 standard.

[edit] Advantages

A variety of advantages can be obtained through the use of stored procedures.

[edit] Pre-compilation of SQL statements

SQL statements implemented as stored procedures may run faster in some circumstances, as they can be pre-compiled. Execution plans for compiled statements can be stored in the database, together with the procedure. That removes the SQL compilation overhead that is typically required in situations where software applications send inline SQL queries to a database.

Pre-compilation implies that the execution plan may not be optimal in all cases. Some information is typically unavailable at the time that the execution plan is generated, for example, the exact values used in predicates. Out-dated statistics are another potential source for suboptimal plans.

The performance benefits of pre-compilation of stored procedures are largely exaggerated. Embedded static SQL is also pre-compiled and, therefore, offers the same advantages without stored procedures. Most database systems implement statement caches to avoid repetitive compilation of dynamic SQL statements.

[edit] Execution on a database server

Stored procedures can run directly within the database engine. In a production system, this typically means that the procedures run entirely on a specialized database server, which has direct access to the data being accessed. The benefit here is that network communication costs can be avoided completely. This becomes particularly important for complex series of SQL statements.

[edit] Simplification of data management

Stored procedures allow for business logic to be embedded as an API in the database, which can simplify data management and reduce the need to encode the logic elsewhere in client programs. This may result in a lesser likelihood of data becoming corrupted through the use of faulty client programs. Thus, the database system can ensure data integrity and consistency with the help of stored procedures.

Some critics claim that databases should be for storing data only, and that business logic should only be implemented by writing a business layer of code, through which client applications should access the data. However, the use of stored procedures does not preclude the use of a business layer.

[edit] Security

Carefully written stored procedures may allow for fine grained security permissions to be applied to a database. For example, client programs might be restricted from accessing the database via any means except those that are provided by the available stored procedures.

[edit] Other uses

In some systems, stored procedures can be used to control transaction management; in others, stored procedures run inside a transaction such that transactions are effectively transparent to them. Stored procedures can also be invoked from a database trigger or a condition handler. For example, a stored procedure may be triggered by an insert on a specific table, or update of a specific field in a table, and the code inside the stored procedure would be executed. Writing stored procedures as condition handlers also allow DBAs to track errors in the system with greater detail by using stored procedures to catch the errors and record some audit information in the database or an external resource like a file.

[edit] External links


Topics in database management systems (DBMS)view  talk  edit )

Concepts
Database • Database model • Relational database • Relational model • Relational algebra • Primary key, Foreign key, Surrogate key, Superkey, Candidate key • Database normalization • Referential integrity • Relational DBMS • Distributed DBMS • ACID

Objects
Trigger • View • Table • Cursor • Log • Transaction • Index • Stored procedure • Partition

Topics in SQL
Select • Insert • Update • Merge • Delete • Join • Union • Create • Drop

Implementations of database management systems

Types of implementations
Relational • Flat file • Deductive • Dimensional • Hierarchical • Object oriented • Temporal • XML data stores

Components
Query language • Query optimizer • Query plan • ODBC • JDBC

Database products

Apache Derby • Berkeley DB • Caché • DB2 • db4o • DBase • eXtremeDB • Filemaker Pro • Firebird • Greenplum • H2 • Hsqldb • Helix • Informix • Ingres • InterBase • Linter • Microsoft Access • Microsoft SQL Server • Mimer SQL • MonetDB • MySQL • Objectivity/DB • OpenLink Virtuoso • OpenOffice.org Base • Oracle • Oracle Rdb • Paradox • Perst • PostgreSQL • SQLite • Sybase IQ • Sybase • Teradata • UniVerse • Visual FoxPro


Other: Object-oriented (comparison) • relational (comparison)