The values we used in the previous lesson are referred to as
constant because we certainly knew them in advance and didn't change them
in our statements. If you intend to use a certain category of value over
and over again, you can reserve a section of memory for that category. This allows you to
put a value in that area of memory, easily change the value with
another, over and over. For example, you can store the names of employees
one after another in the same area of memory as needed. This exchange of
values with the computer memory can be managed by the
interpreter.
To use the same area of memory to store and remove
values as needed, the interpreter needs two primary pieces of information:
a name and the desired amount of space in memory capable of storing the
value.
Declaring Variables in MSDE
|
|
A variable is an area of memory used to store values
that can be used in a program. Before using a variable, you must inform
the interpreter. This is also referred to as declaring a variable. To
declare a variable, if you are using MSDE, use the DECLARE keyword using the following
formula:
DECLARE Options
The DECLARE keyword lets the interpreter know
that you are making a declaration. The DECLARE keyword is followed
by a name for the variable. In MSDE, the name of a variable starts
with the @ sign. The name of a variable allows you to identify the area of
memory where the value of the variable is stored. While other languages
like C/C++, Pascal, Java, C#, etc impose strict rules to names,
Transact-SQL is extremely flexible. A name can be made of digits only.
Here is an example:
DECLARE @264
Such a name made of digits can create confusion
with a normal number. A name can also be made of one or more words.
To
avoid confusion, here are the rules we will use on this site:
- A name will start with either an underscore or a letter. Examples
are @_n, @act, or @Second
- After the first character as an underscore or a letter, the name
will have combinations of underscores, letters, and digits. Examples
are @_n24, @act_52_t
- A name will not include special characters such as !, @, #, $, %, ^,
&, or *
- We will avoid using spaces in a name, with few exceptions
- If the name is a combination of words, each word will start in
uppercase. Examples are @DateHired, @_RealSport, or @DriversLicenseNumber
To declare a variable, if you are using MSDE, as we will see in the next
sections, after giving a name to a variable, you must also specify the
amount of memory that the variable would need. The amount of memory is
also called a data type. Therefore, the declaration of a variable in MSDE uses the
following formula:
DECLARE @VariableName DataType
With MSDE, you can also declare more than one
variable. To do
that, separate them with a comma. The formula would be:
DECLARE @Variable1 DataType, @Variable2 DataType, @Variable1 DataType
Unlike many other languages like C/C++ or Pascal, if
you declare many variables that use the same data type, the name of each
variable must be followed by its own data type.
Initializing a Variable in MSDE
|
|
After declaring a variable, the
interpreter reserves a space in the computer memory for it but the space doesn't
necessarily hold a recognizable value. This means that, at this time, the
variable is null. One way you can change this is to give a value to the
variable. This is referred to as initializing the variable.
Remember
that a variable's name starts with @ and whenever you need to refer to the
variable, you must make sure you include the @ sign. To initialize a
variable, in the necessary section, type the SELECT or the SET
keyword followed by the name of the variable, followed by the assignment
operator "=",
followed by an appropriate value. The formula used is:
SELECT @VariableName = DesiredValue
or
SET @VariableName = DesiredValue
Once a variable has been initialized, you can make its
value available, or display it, to the user. This time, you can type the
name of the variable to the right side of PRINT or SELECT.
|
|
No comments:
Post a Comment