DECLARE Statement

Declares an SQL variable within a compound statement (BEGIN... END).

Quick Links:

Go to Parameters

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

DECLARE 
   variable_name  [ , … ] 
   data-type  [{
   = 
   | DEFAULT} 
   initial-value]

initial-value - (back to Syntax)
   special-value 
  | string 
  | [ - ] number 
  | ( constant-expression ) 
  | built-in-function ( constant-expression ) 
  | NULL

special-value - (back to initial-value)
   CURRENT 
   { DATABASE 
    | DATE 
    | PUBLISHER 
    | TIME 
    | TIMESTAMP 
    | USER 
    | UTC TIMESTAMP } 
   | USER

Parameters

(back to top)

Examples

(back to top)

Usage

(back to top)

Use the DECLARE statement to declare variables used in the body of a procedure. The variable persists for the duration of the compound statement in which it is declared and must be unique within the compound statement.

The body of a procedure is a compound statement, and variables must be declared immediately following BEGIN. In a Transact-SQL procedure or trigger, there is no such restriction.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Sybase Database product—Supported by Adaptive Server.
    • To be compatible with Adaptive Server, the variable name must be preceded by an @.
    • In Adaptive Server, a variable that is declared in a procedure or trigger exists for the duration of the procedure or trigger. In SAP Sybase IQ, if a variable is declared inside a compound statement, it exists only for the duration of that compound statement (whether it is declared in an SAP Sybase IQ SQL or Transact-SQL compound statement).

Permissions

(back to top)

None

Related reference
BEGIN … END Statement