[ Team LiB ] |
Lab 3.1 Exercises3.1.1 Make Use of PL/SQL Language ComponentsNow that you have the character types and the lexical units, it is equivalent to knowing the alphabet and how to spell out words.
3.1.2 Make Use of PL/SQL VariablesVariables may be used to hold a temporary value.
Variables may also be known as identifiers. There are some restrictions that you need to be familiar with: Variables must begin with a letter and may be up to 30 characters long. Consider the following example: FOR EXAMPLE This example contains a list of valid identifiers:
It is important to note that the identifiers v_last_name and V_LAST_NAME are considered identical because PL/SQL is not case sensitive. Next, consider an example of illegal identifiers: FOR EXAMPLE
Identifier X+Y is illegal because it contains the "+" sign. This sign is reserved by PL/SQL to denote an addition operation, and it is referred to as a mathematical symbol. Identifier, 1st_year is illegal because it starts with a number. Finally, identifier student ID is illegal because it contains a space. Next, consider another example: FOR EXAMPLE
In this example, you declare a variable called first&last_names. Next, you assign a value to this variable and display this value on the screen. When run, the example produces the following output:
Consider the output produced. Because there is an ampersand (&) present in the name of the variable first&last_names, the portion of the variable is considered to be a substitution variable (you learned about substitution variables in Chapter 2). In other words, the portion of the variable name after the ampersand (last_names) is treated by the PL/SQL compiler as a substitution variable. As a result, you are prompted to enter the value for the last_names variable every time the compiler encounters it. It is important to realize that while this example does not produce any syntax errors, the variable first&last_names is still an invalid identifier because the ampersand character is reserved for substitution variables. To avoid this problem, change the name of the variable from first&last_names to first_and_last_names. Therefore, you should use an ampersand sign in the name of a variable only when you use it as a substitution variable in your program. FOR EXAMPLE
3.1.3 Handle PL/SQL Reserved WordsReserved words are ones that PL/SQL saves for its own use (e.g., BEGIN, END, and SELECT). You cannot use reserved words for names of variables, literals, or user-defined exceptions. FOR EXAMPLE
3.1.4 Make Use of Identifiers in PL/SQLTake a look at the use of identifiers in the following example: FOR EXAMPLE
In this example, you declare and initialize three variables. The values that you assign to them are literals. The first two values, 'string literal' and '12.345' are string literals because they are enclosed by single quotes. The third value, 12.345, is a numeric literal. When run, the example produces the following output:
Consider another example that uses numeric literals: FOR EXAMPLE
3.1.5 Make Use of Anchored Data TypesThe data type that you assign to a variable can be based on a database object. This is called an anchored declaration since the variable's data type is dependent on that of the underlying object. It is wise to make use of anchored data types when possible so that you do not have to update your PL/SQL when the data types of base objects change.
The type is a direct reference to a database column. FOR EXAMPLE
3.1.6 Declare and Initialize VariablesIn PL/SQL, variables must be declared in order to be referenced. This is done in the initial declarative section of a PL/SQL block. Remember that each declaration must be terminated with a semicolon. Variables can be assigned using the assignment operator ":=". If you declare a variable to be a constant, it will retain the same value throughout the block; in order to do this, you must give it a value at declaration. Type the following into a text file and run the script from a SQL*Plus session.
FOR EXAMPLE
PL/SQL variables are held together with expressions and operators. An expression is a sequence of variables and literals, separated by operators. These expressions are then used to manipulate data, perform calculations, and compare data. Expressions are composed of a combination of operands and operators. An operand is an argument to the operator; it can be a variable, a constant, a function call. An operator is what specifies the action (+, **, /, OR, etc.). You can use parentheses to control the order in which Oracle evaluates an expression. Continue to add the following to your SQL script the following:
3.1.7 Understand the Scope of a Block, Nested Blocks, and LabelsScope of a VariableThe scope, or existence, of structures defined in the declaration section are local to that block. The block also provides the scope for exceptions that are declared and raised. Exceptions will be covered in more detail in Chapters 7, 10, and 11. The scope of a variable is the portion of the program in which the variable can be accessed, or where the variable is visible. It usually extends from the moment of declaration until the end of the block in which the variable was declared. The visibility of a variable is the part of the program where the variable can be accessed.
Labels and Nested BlocksLabels can be added to a block in order to improve readability and to qualify the names of elements that exist under the same name in nested blocks. The name of the block must precede the first line of executable code (either the BEGIN or DECLARE) as follows: FOR EXAMPLE
The label optionally appears after END. In SQL*Plus, the first line of a PL/SQL block cannot be a label. For commenting purposes, you may alternatively use "- -" or /*, ending with */. Blocks can be nested in the main section or in an exception handler. A nested block is a block that is placed fully within another block. This has an impact on the scope and visibility of variables. The scope of a variable in a nested block is the period when memory is being allocated for the variable and extends from the moment of declaration until the END of the nested block from which it was declared. The visibility of a variable is the part of the program where the variable can be accessed. FOR EXAMPLE
This example produces the following output:
|
[ Team LiB ] |
No comments:
Post a Comment