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 ...

Get Oracle PL/SQL Programming: A Developer's Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.