Stored Procedure Overview | Teradata Vantage - Stored Procedure Overview - Advanced SQL Engine - Teradata Database

SQL Stored Procedures and Embedded SQL

Product
Advanced SQL Engine
Teradata Database
Release Number
17.05
17.00
Published
June 2020
Language
English (United States)
Last Update
2021-01-23
dita:mapPath
xqq1557098602407.ditamap
dita:ditavalPath
lze1555437562152.ditaval
dita:id
B035-1148
lifecycle
previous
Product Category
Teradata Vantage™

Definition

A stored procedure, a database object executed on the Teradata Database, is a combination of SQL statements and control and condition handling statements that provide an interface to the Teradata Database.

The term stored procedure refers to a stored procedure you write with SQL statements. The term external stored procedure refers to a stored procedure you write in C, C++, or Java.
Typically, a stored procedure consists of a:
  • Procedure name
  • Input and output parameters
  • Procedure body.

For each stored procedure, the database includes a stored procedure table that contains the stored procedure body you write and the corresponding compiled stored procedure object code. Data dictionary tables contain stored procedure parameters and attributes.

Procedure Body and Source Text

The following terms are useful in understanding the structure of a stored procedure.

Term Definition
Procedure body The set of statements constituting the main tasks of the stored procedure.

The procedure body can be a single control statement or SQL statement, or a BEGIN … END compound statement (sometimes called a block). Compound statements can also be nested.

Source text The entire definition of a stored procedure, including the CREATE/REPLACE PROCEDURE statement, parameters, procedure name, and the stored procedure body.

Elements in a Procedure Body

A procedure body can contain the following elements:

Stored procedure body of this type … Can contain …
Single statement one SQL statement or control statement, including dynamic SQL.
The following elements are not allowed:
  • Any declaration (local variable, condition, cursor, or condition handler) statement
  • A cursor statement (OPEN, FETCH, or CLOSE)
Compound statement
  • Local variable declarations
  • Condition declarations
  • Cursor declarations
  • Condition handler declarations
  • Control statements
  • SQL Data Manipulation Language (DML), Data Definition Language (DDL), and Data Control Language (DCL) statements supported by stored procedures, including dynamic SQL.

You do not have to enclose a compound statement within BEGIN … END keywords if the procedure body contains only one statement and does not contain any declarations.

Stored Procedure Benefits

A stored procedure provides control and condition handling statements, in addition to multiple input and output parameters and local variables, that make SQL a computationally complete programming language.

Applications based on stored procedures provide the following benefits over equivalent embedded SQL applications:
  • Better performance because of greatly reduced network traffic between the client and server.
  • Better application maintenance because business rules are encapsulated and enforced on the server.
  • Better transaction control.
  • Better application security by restricting user access to procedures rather than requiring them to access data tables directly.
  • Better application execution because all SQL language statements are embedded in a stored procedure to be executed on the server through one CALL statement.

    Nested CALL statements extend performance by combining all transactions and complex queries in the nested procedures into one explicit transaction, and by handling errors internally in the nested procedures.

Related Topics

  • For more information about stored procedures, see SQL Stored Procedures.
  • For information about stored procedure control statements, see SQL Control Statements.
  • For information about external stored procedures, see Teradata Vantage™ - SQL Data Definition Language Detailed Topics , B035-1184 and Teradata Vantage™ - SQL External Routine Programming , B035-1147 .
  • For information on the syntax for creating stored procedures, see “CREATE/REPLACE PROCEDURE” in Teradata Vantage™ - SQL Data Definition Language Syntax and Examples, B035-1144.