Chapter 6 SQL Language Elements
251
♦ The variable
local_var
does not exist outside the compound statement in
which it is declared. The following batch is invalid, and gives a
column
not found
error.
-- This batch is invalid.
BEGIN
DECLARE local_var INT ;
SET local_var = 10 ;
MESSAGE ’local_var = ’, local_var TO CLIENT;
END;
MESSAGE ’local_var = ’, local_var TO CLIENT;
♦ The following example illustrates the use of SELECT with an INTO
clause to set the value of a local variable:
BEGIN
DECLARE local_var INT ;
SELECT 10 INTO local_var ;
MESSAGE ’local_var = ’, local_var TO CLIENT;
END
Running this batch from Interactive SQL gives the message local_var =
10 on the server window.
♦
Names Adaptive Server Enterprise and Adaptive Server Anywhere
both support local variables. In Adaptive Server Enterprise, all variables
must be prefixed with an @ sign. In Adaptive Server Anywhere, the @
prefix is optional. To write compatible SQL, prefix all of your variables
with @.
♦
Scope The scope of local variables is different in Adaptive Server
Anywhere and Adaptive Server Enterprise. Adaptive Server Anywhere
supports the use of the DECLARE statement to declare local variables
within a batch. However, if the DECLARE is executed within a
compound statement, the scope is limited to the compound statement.
♦
Declaration Only one variable can be declared for each DECLARE
statement in Adaptive Server Anywhere. In Adaptive Server Enterprise,
more than one variable can be declared in a single statement.
$ For more information on batches and local variable scope, see
"Variables in Transact-SQL procedures" on page 985 of the book ASA User’s
Guide.
Connection-level variables
Connection-level variables are declared with the CREATE VARIABLE
statement. Connection-level variables can be passed as parameters to
procedures.
Compatibility