Chapter 1. Declaring Variables and Naming Elements

Beginner

Q:

1-1.

The following variables are valid or invalid for these reasons:

  1. Valid. my_variable2 starts with a letter, is less than 31 characters in length, and contains only letters, digits, and $, #, or _.

  2. Invalid. my-variable2 may not contain a dash.

  3. Invalid. my^variable contains an illegal character, ^.

  4. Valid. MyVariable starts with a letter, is less than 31 characters in length, and contains only letters, digits, and $, #, or _.

  5. Invalid. my_variable_for_many_many_of_usages contains more than 30 characters.

  6. Invalid. 123myvariable cannot start with a number.

  7. Valid. “123myvariable” is surrounded by double quotes. If you surround an identifier with double quotes (very different from two consecutive single quotes), then all rules about identifiers are suspended except for the maximum length of 30 characters.

Q:

1-2.

Oddly enough, (a) compiles, while (b) fails with the following error message:

PLS-00371: at most one declaration for 'LASTDATE' is permitted

PL/SQL is a case-insensitive language (except for the contents of literal strings). Therefore, in both cases you are trying to declare two variables with the same name, which is not allowed. It turns out, however, that the compiler will not reject the duplicate declarations unless you actually try to use one of the variables!

Q:

1-3.

Here is an example that demonstrates these two formats and shows that the same value is assigned:

DECLARE
  myDate DATE := SYSDATE;
  yourDate DATE DEFAULT SYSDATE;
BEGIN
  DBMS_OUTPUT.PUT_LINE
     ('The date value in myDate is '||mydate);
  DBMS_OUTPUT.PUT_LINE
     ('The date value in yourDate is '||yourdate);
END;
/
The date value in myDate is 29-OCT-99
The date value in yourDate is 29-OCT-99

Q:

1-4.

DECLARE
    my_name employee.last_name%TYPE;

Q:

1-5.

These declarations are invalid for the following reasons:

Invalid Code

Problem

Valid Code

INTEGER year_count;

The datatype must come after the identifier.

year_count INTEGER;
VARCHAR2(100)
company_name,
employee_name;

The datatype must come after the identifier, and you can declare only one variable per logical statement.

company_name
VARCHAR2(100);
employee_name
VARCHAR2(100);
delimiters VARCHAR2;

You must provide a constraint (maximum number of characters) for a VARCHAR2 declaration unless you are declaring a parameter.

delimiters
VARCHAR2(100);
curr_year CONSTANT
INTEGER;

If you declare a variable to be a constant, you must provide a default value.

curr_year CONSTANT
INTEGER
    := TO_NUMBER
    (TO_CHAR
    (SYSDATE, 'YYYY'));
invalid_date
EXCEPTION
   := VALUE_ERROR;

You can declare your own exceptions, but they cannot be given default or initial values.

invalid_date EXCEPTION;

Q:

1-6.

These are the most appropriate datatypes for these data items:

Data Item

Datatype

  1. ‘This is a test’

VARCHAR2 or CHAR

  1. Values from 1 to 10

PLS_INTEGER (best performance), INTEGER, BINARY_INTEGER, NATURAL, POSITIVE

  1. 5.987

NUMBER

  1. ‘This is a test’

CHAR;. you need a fixed-length declaration to preserve the spaces

  1. A string that is always nine
    characters long

CHAR

  1. January 10, 2000

DATE

  1. A binary file

BFILE (in Oracle8); prior to Oracle8, LONG or LONG RAW

  1. TRUE or FALSE

BOOLEAN

  1. The internal identifier of a
    row in a table

ROWID

Intermediate

Q:

1-7.

Use the %TYPE anchoring attribute against a PL/SQL variable, just as you would anchor to the column of a table:

CREATE OR REPLACE PACKAGE types
IS
 dollar_amount NUMBER(20,2);
END;
/
DECLARE
   my_dollars types.dollar_amount%TYPE;
BEGIN
   ...
END;

Q:

1-8.

The statements about the DATE datatype are:

  1. Both true and false. Prior to Oracle 7.3, the maximum date was January 1, 4712. In later versions of Oracle, the maximum date has now been set to December 31, 9999.

  2. True

  3. True. The Oracle DATE is really a date-time data structure.

  4. False. A date variable records the time down only to the nearest second.

  5. False. No matter how you specify the date value, the internal format always uses a four-digit year.

Q:

1-9.

The thing to remember when using a SUBTYPE is that you do not include a %TYPE anchoring attribute. A subtype already is a type. Here is the solution:

CREATE OR REPLACE PACKAGE types
IS
   SUBTYPE dollar_amount_t IS NUMBER;
END;
/
DECLARE
   my_dollars types.dollar_amount_t;
BEGIN
   ...
END;

Q:

1-10.

You can’t anchor (use %TYPE) against a CONSTANT; it must be a variable.

Q:

1-11.

Unlike the folks who wrote the PL/SQL language, we developers are not allowed to “constrain” our own SUBTYPEs. In other words, after the IS keyword you cannot supply a datatype declaration that limits the size or length explicitly. Note that this restriction is relaxed in Oracle8i .

Q:

1-12.

You receive this error:

ORA-06502: PL/SQL: numeric or value error

because a variable assigned the type SIGNTYPE can have only one of three values: –1, 1, or NULL.

Q:

1-13.

Statements (b) and (c) both describe the value of anchoring.

Q:

1-14.

You can anchor to a table, a view, a column in a table or view, a cursor, or a scalar PL/SQL variable.

Q:

1-15.

One would hope that this function returns a BOOLEAN, as in:

FUNCTION is_value_in_list (list IN VARCHAR2, value IN VARCHAR2)
   RETURN BOOLEAN;

Oracle Forms documentation for the GET_GROUP_CHAR_CELL function unfortunately offers an example program named is_value_in_list that returns a number. If you name programs inaccurately, developers will have a much harder time understanding and using those programs.

Expert

Q:

1-16.

THEN is a reserved word; the PL/SQL compiler refuses to interpret it as a variable name. SYSDATE, on the other hand, is not a reserved word. Rather, it is a function declared in the STANDARD package, one of the two default packages of PL/SQL. You could write that block in an even more confusing manner, just to drive home the difference between “your” sysdate variable and the STANDARD’s SYSDATE function:

DECLARE
   sysdate DATE;
BEGIN
   sysdate := sysdate;
   DBMS_OUTPUT.PUT_LINE ('Date is ' || sysdate);
   sysdate := STANDARD.SYSDATE;
   DBMS_OUTPUT.PUT_LINE ('Date is ' || sysdate);
END;

You will see this output:

Date is
Date is 24-JAN-99

As explained in 1-11, we developers are not allowed to constrain our own SUBTYPEs. In other words, you cannot supply after the IS keyword a datatype declaration that limits the size or length explicitly. Check out $ORACLE_HOME/RdbmsNN/admin/standard.sql (the file that creates the PL/SQL STANDARD package) for examples of constrained SUBTYPEs.

Q:

1-17.

The following block of code raises a VALUE_ERROR exception when executed. It demonstrates the technique of constraining a SUBTYPE:

DECLARE
   primary_key NUMBER(6);
   SUBTYPE primary_key_t IS primary_key%TYPE;
   mypky primary_key_t;
BEGIN
   mypky := 11111111;
END;

What you’ve done is a sleight-of-hand maneuver. You want the SUBTYPE declaration to look like this:

SUBTYPE primary_key_t IS NUMBER(6);

But that code will be rejected by the compiler. Instead, you must declare a variable with the appropriate constraint and then reference that variable with a %TYPE in your SUBTYPE statement. The subtype then inherits the constraint.

Q:

1-18.

Here are three rules to keep in mind when working with NULLs:

  • For all operators except for concatenation (the || symbol), if a value in an expression is NULL, that expression evaluates to NULL.

  • NULL is never equal or not equal to another value.

  • NULL is never TRUE or FALSE.

Q:

1-19.

These operators allow you to work with NULLs in a structured way:

NVL

Converts a NULL to another specified value, as in:

myValue := NVL (yourValue, 'WHOOPS'),
IS NULL and IS NOT NULL

You can use this syntax to check specifically to see if a variable’s value is NULL or NOT NULL.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.42.205