Вы находитесь на странице: 1из 6

Part I. Problems Chapter 1.

Declaring Variables and Naming Elements


Variables allow you to store and manipulate datasuch as column values, counters, or calculations inside your PL/SQL programs. There are two types of variables: scalar, which are made up of a single value, and composite, which are made up of multiple pieces (a record, for example, is represented with a composite variable). Whether composite or scalar, every variable has a name (also called an identifier), a datatype (such as NUMBER or VARCHAR2), and a value. This chapter tests your ability to work with these most basic PL/SQL elements.

Intermediate
1-7. Declare a local variable to have the same datatype as the dollar_amount variable in the types package. 1-8. Which of the following statements about the DATE datatype are true? 1. The maximum (latest) date allowed in a date variable is January 1, 4712. 2. The earliest date allowed in a date variable is January 1, 4712 BC (or BCE). 3. A date variable contains both date and time information. 4. A date variable records the time down to the nearest hundredth of a second. 5. The DATE datatype stores only those numbers of digits for the year you specify when you assign a value to the variable. 1-9. Declare a local variable to have the same datatype as the dollar_amount_t SUBTYPE in the types package. 1-10. Explain what is wrong with the following anchored declaration:
DECLARE dollar_amt CONSTANT NUMBER (20, 2) := 0; other_dollar_amt dollar_amt%TYPE;

1-11. Explain what is wrong with the following SUBTYPE declaration:


DECLARE SUBTYPE small_number IS NUMBER (3);

1-12. What error is raised when you try to execute this block of code? What is the problem?
DECLARE your_choice SIGNTYPE := 2; BEGIN NULL; END;

1-13. Which of the following statements describe accurately how the anchoring of a variables datatype to a data structure improves the maintainability of that code? 1. To anchor, you have to use %TYPE or %ROWTYPE, which involves more typing and that means you have more time to think about what you are typing and can get it right. 2. If you anchor against a table or column in a table, when that table changes, your compiled code will be marked invalid. After recompilation, your code automatically reflects the latest structure in the table. 3. When you anchor against a PL/SQL variable, you make sure to define your type of data only once and then reuse that definition. So if the definition changes, you have to change your code in only one place. 1-14. Name at least three different objects to which you could anchor a variable. 1-15. What datatype would you expect a function of the name is_value_in_list to return?

Expert
1-16. Why does this code compile?
DECLARE sysdate NUMBER; BEGIN sysdate := 1; END;

But this code does not?


DECLARE then NUMBER; BEGIN then := 1; END;

1-17. How can you get around this restriction and declare SUBTYPEs which are, in effect, constrained? To be specific, I want to declare a subtype called primary_key_t that limits the size of any variable declared with that type to NUMBER(6).

1-18. The NULL value in Oracle is handled differently from other values. One could even say that there is no such thing as a NULL value. (NULL means indeterminate.) Name three rules that apply to NULLs when doing comparisons. 1-19. What special operators does Oracle provide for dealing with NULLs?

==============Solution=============== 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 fourdigit 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 cant 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. 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 STANDARDs 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 youve 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 variables value is NULL or NOT NULL.

Вам также может понравиться