Variables
252
The syntax for the CREATE VARIABLE statement is as follows:
CREATE VARIABLE
variable-name
data-type
When a variable is created, it is initially set to NULL. The value of
connection-level variables can be set in the same way as local variables,
using the SET statement or using a SELECT statement with an INTO clause.
Connection-level variables exist until the connection is terminated, or until
the variable is explicitly dropped using the DROP VARIABLE statement.
The following statement drops the variable con_var:
DROP VARIABLE con_var
♦ The following batch of SQL statements illustrates the use of connection-
level variables.
CREATE VARIABLE con_var INT;
SET con_var = 10;
MESSAGE ’con_var = ’, con_var TO CLIENT;
Running this batch from Interactive SQL gives the message con_var =
10 on the server window.
♦ Adaptive Server Enterprise does not support connection-level variables.
Global variables
Global variables have values set by the database server. For example, the
global variable @@version has a value that is the current version number of
the database server.
Global variables are distinguished from local and connection-level variables
by having two @ signs preceding their names. For example, @@error and
@@rowcount are global variables. Users cannot create global variables, and
cannot update the value of global variables directly.
Some global variables, such as @@identity, hold connection-specific
information, and so have connection-specific values. Other variables, such as
@@connections, have values that are common to all connections.
The special constants (for example, CURRENT DATE, CURRENT TIME,
USER, and SQLSTATE) are similar to global variables.
The following statement retrieves a value of the version global variable.
SELECT @@version
In procedures and triggers, global variables can be selected into a variable
list. The following procedure returns the server version number in the ver
parameter.
Example
Compatibility
Global variable and
special constants