SQL Variable Statements
SQL variable statements provide a means to store values internally from statement to statement. SQL variable statements include the following statement:
You can use these statements inside stored procedures.
Procedure-Owned Variables
An SQL variable you define inside a stored procedure is a procedure-owned variable. Its scope is that procedure in which it is declared; you can only refer to it within that procedure. If a procedure calls another procedure, the procedure-owned variable of the calling procedure cannot be directly used in the called procedure; instead, it must be passed in a parameter. You cannot declare a procedure-owned variable more than once in the same stored procedure.
If a compound statement is the body of a stored procedure, then no SQL variable name declared in that procedure can be identical to a parameter name in the parameter list of that procedure. For more information about compound statements, refer to Compound Statement.
Assignment Statement
The assignment statement initializes or changes the values of SQL variables. The value expression may be a computed expression involving constants, operators, and this or other SQL variables.
SET :CourseName = 'HIS305';
The value expression may also be a SELECT statement.
SET :MaxEnrollment = (SELECT Max_Size FROM Class
WHERE ID = classId);
For more information about the syntax of this statement, refer to the following topic in SQL Engine Reference: SET.