Teste Bd

16
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners. Section 1 Lesson 1: Introduction to PL/SQL Vocabulary Identify the vocabulary word for each definition below: Oracle Corporations standard procedural language for relational databases which allows basic program logic and control flow to be combined with SQL statements Try It / Solve It 1. Circle the programming language meeting the criteria Criteria Language 3GL PL/SQL SQL 4GL PL/SQL SQL Is proprietary to Oracle Corporation PL/SQL SQL Nonprocedural PL/SQL SQL Procedural PL/SQL SQL Is ANSI-compliant PL/SQL SQL 2. In your own words, describe why a procedural language like PL/SQL is needed. 3. Define a procedural construct. 4. List some examples of procedural constructs in PL/SQL. 5. In the following code, identify and circle examples of these procedural constructs: variable, conditional control, SQL statement DECLARE v_first_name varchar2(40); v_last_name varchar2(40); v_first_letter varchar2(1); BEGIN SELECT first_name, last_name into v_first_name, v_last_name FROM students WHERE student_id=105; v_first_letter := get_first_letter(last_name); IF 'N' > 'v_first_letter' THEN DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||' '||last_name||' is between A and M'); ELSE DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||' '||last_name||' is between N and Z'); END IF; END;

description

TEST ORACLE

Transcript of Teste Bd

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 1 Lesson 1: Introduction to PL/SQL

    Vocabulary

    Identify the vocabulary word for each definition below:

    Oracle Corporations standard procedural language for relational databases which allows basic program logic and control flow to be combined with SQL statements

    Try It / Solve It 1. Circle the programming language meeting the criteria

    Criteria Language 3GL PL/SQL SQL 4GL PL/SQL SQL Is proprietary to Oracle Corporation PL/SQL SQL Nonprocedural PL/SQL SQL Procedural PL/SQL SQL Is ANSI-compliant PL/SQL SQL

    2. In your own words, describe why a procedural language like PL/SQL is needed. 3. Define a procedural construct. 4. List some examples of procedural constructs in PL/SQL. 5. In the following code, identify and circle examples of these procedural constructs:

    variable, conditional control, SQL statement

    DECLARE v_first_name varchar2(40); v_last_name varchar2(40); v_first_letter varchar2(1); BEGIN SELECT first_name, last_name into v_first_name, v_last_name FROM students WHERE student_id=105; v_first_letter := get_first_letter(last_name); IF 'N' > 'v_first_letter' THEN

    DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||' '||last_name||' is between A and M');

    ELSE DBMS_OUTPUT.PUT_LINE('The last name for: '||first_name||' '||last_name||' is

    between N and Z'); END IF; END;

    AdyTypewritten TextPL/SQL

    AdyTypewritten Text

    AdyStamp

    AdyStamp

    AdyStamp

    AdyStamp

    AdyStamp

    AdyStamp

    AdyTypewritten TextAllows basic program logic and control flow to be combined with SQL statements.

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextIt can be used only with an Oracle database or tool

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextYou use PL/SQL to write the procedural code, and embed SQL data-accessing statements within the

    AdyTypewritten TextVariables, constants, and types,

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextControl structures, such as conditional statements and loops

    AdyTypewritten Text

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 1 Lesson 2: Benefits of PL/SQL

    Vocabulary

    Identify the vocabulary word for each definition below:

    The ability for PL/SQL programs to run anywhere an Oracle server runs.

    The basic unit of PL/SQL programs- also known as modules.

    An error that occurs in the database or in a users program during runtime.

    Try It / Solve It

    1. Why is it more efficient to combine SQL statements into PL/SQL blocks? 2. Why is it beneficial to use PL/SQL with an Oracle database? List at least three reasons. 3. How is PL/SQL different from C and Java? List three differences. 4. List three examples of what you can build with PL/SQL code.

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextYou can group logically related statements within blocks.You can nest blocks inside other blocks to build pwf prg.

    AdyTypewritten Text

    AdyTypewritten TextYou can share blocks with other programmers to speed up development time

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextPortability

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextException Handling

    AdyTypewritten TextRequires Oracle database or tool;

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextPerformance against an Oracle database;

    AdyTypewritten TextEase of learning

    AdyTypewritten TextYou can write PL/SQL code to manage application data or to manage the Oracle database itself. For example, you can write code for updating data (DML), creating data (DDL), generating reports, managing security, and so on.

    AdyTypewritten TextUsing the Web Application Toolkit, you can create database-centric web applications written entirely or partially in PL/SQL.

    AdyTypewritten TextUsing a Web browser you can develop web applications that include PL/SQL.

    AdyTypewritten Text1.The application can send the entire block to the database instead of sending the SQL statements one at a time. This significantly reduces the number of database calls (consider a database with several million records).

    AdyTypewritten TextModularized Program Development

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 1 Lesson 3: Creating PL/SQL Blocks

    Vocabulary

    Identify the vocabulary word for each definition below:

    Unnamed blocks of code not stored in the database and do not exist after they are executed

    A program that computes and returns a value

    Named PL/SQL blocks that are stored in the database and can be declared as procedures or functions

    Software that checks and translates programs written in high-level programming languages into binary code to execute

    A program that performs an action and does not have to return a value

    Try It / Solve It

    1. Complete the following chart defining the syntactical requirements for a PL/SQL block:

    Optional or Mandatory? Describe what is included in this section

    DECLARE

    BEGIN

    EXCEPTION

    END;

    2. Which of the following PL/SQL blocks executes successfully? For the blocks that fail,

    explain why they fail

    A. BEGIN END;

    B. DECLARE

    amount INTEGER(10); END;

    C. DECLARE

    BEGIN END;

    D. DECLARE

    amount NUMBER(10); BEGIN DBMS_OUTPUT.PUT_LINE(amount); END;

    AdyTypewritten Textoptional

    AdyTypewritten Text

    AdyTypewritten Textvariabile,constante,cursori.

    AdyTypewritten Text

    AdyTypewritten Textmandatory

    AdyTypewritten Text

    AdyTypewritten Textsql statements to retrieve data.

    AdyTypewritten Textoptional

    AdyTypewritten Textactiuni atunci cand sunt erori sau conditii anormale

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Textin sectiunea executabila

    AdyTypewritten Text

    AdyTypewritten Textmandatory

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextCompiler

    AdyTypewritten TextAnonymous Blocks

    AdyTypewritten TextSubprograms

    AdyTypewritten Text

    AdyTypewritten TextFunction

    AdyTypewritten TextProcedure

    AdyTypewritten TextNo declaration or exception sections

    AdyTypewritten TextDeclaration and execution sections, but no exception section

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    3. Fill in the blanks:

    A. PL/SQL blocks that have no names are called __________________.

    B. _______________ and _______________ are named blocks and are stored in the database.

    4. In Application Express, create and execute a simple anonymous block that outputs Hello

    World.

    Extension Exercise

    1. Create and execute a simple anonymous block that does the following:

    Declares a variable of datatype DATE and populates it with the date that is six months from today

    Outputs In six months, the date will be: .

    AdyTypewritten TextAnonymous Blocks

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextProcedure

    AdyTypewritten TextFunction

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 1: Using Variables in PL/SQL

    Vocabulary Identify the vocabulary word for each definition below:

    Used for storage of data and manipulation of stored values.

    values passed to a program by a user or by another program to customize the program.

    Try It / Solve It

    1. Fill in the blanks.

    A. Variables can be assigned to the output of a __________________.

    B. Variables can be assigned values in the _____________________ section of a PL/SQL block.

    C. Variables can be passed as ____________________ to subprograms.

    2. Identify valid and invalid variable declaration and initialization:

    number_of_copies PLS_INTEGER; printer_name CONSTANT VARCHAR2(10); deliver_to VARCHAR2(10):=Johnson; by_when DATE:= SYSDATE+1;

    3. Examine the following anonymous block and choose the appropriate statement.

    DECLARE fname VARCHAR2(20); lname VARCHAR2(15) DEFAULT 'fernandez'; BEGIN DBMS_OUTPUT.PUT_LINE( FNAME ||' ' ||lname); END; A. The block will execute successfully and print fernandez. B. The block will give an error because the fname variable is used without initializing. C. The block will execute successfully and print null fernandez. D. The block will give an error because you cannot use the DEFAULT keyword to

    initialize a variable of the VARCHAR2 type. E. The block will give an error because the FNAME variable is not declared.

    AdyTypewritten TextPL/SQL subprogram

    AdyTypewritten Textexecutable

    AdyTypewritten Textparameters

    AdyTypewritten TextDeclaring Variables

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextParameters

    AdyTypewritten Textinvalid

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Textinvalid

    AdyTypewritten Textvalid

    AdyTypewritten Textinvalid

    AdyTypewritten Text

    AdyTypewritten Text

    AdyStamp

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    4. In Application Express:

    A. Create the following function:

    CREATE FUNCTION num_characters (p_string IN VARCHAR2) RETURN INTEGER AS v_num_characters INTEGER; BEGIN SELECT LENGTH(p_string) INTO v_num_characters FROM dual; RETURN v_num_characters; END;

    B. Create and execute the following anonymous block:

    DECLARE v_length_of_string INTEGER; BEGIN v_length_of_string := num_characters('Oracle Corporation'); DBMS_OUTPUT.PUT_LINE(v_length_of_string); END;

    5. Write an anonymous block that uses a country name as input and prints the highest and

    lowest elevations for that country. Use the wf_countries table. Execute your block three times using United States of America, French Republic, and Japan.

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 2: Recognizing PL/SQL Lexical Units Vocabulary Identify the vocabulary word for each definition below:

    An explicit numeric, character string, date, or Boolean value that is not represented by an identifier.

    Symbols that have special meaning to an Oracle database.

    Words that have special meaning to an Oracle database and cannot be used as identifiers.

    Describe the purpose and use of each code segment and are ignored by PL/SQL.

    Building blocks of any PL/SQL block and are sequences of characters including letters, digits, tabs, returns, and symbols.

    A name, up to 30 characters in length, given to a PL/SQL object.

    Try It / Solve It Questions

    1. Fill in the blanks. A. An ___________________ is the name given to a PL/SQL object.

    B. A ____________________ is a word that has special meaning to the Oracle database.

    C. A ____________________ is a symbol that has special meaning to the Oracle

    database.

    D. A ____________________ is an explicit numeric, character string, date, or Boolean value that is not represented by an identifier.

    E. A_____________________ explains what a piece of code is trying to achieve.

    2. Identify each of the following identifiers as valid or invalid. If invalid, specify why.

    Identifier Valid (X)

    Invalid (X)

    Why Invalid?

    Today

    Last name

    todays_date number_of_days_in_february_this_ year

    Isleap$year

    #number

    NUMBER#

    Number1to7

    AdyTypewritten Textidentifier

    AdyTypewritten TextIdentifier

    AdyTypewritten TextReserved words

    AdyTypewritten TextReserved words

    AdyTypewritten Text

    AdyTypewritten TextDelimiters

    AdyTypewritten TextDelimiters

    AdyTypewritten Textliteral

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Textliteral

    AdyTypewritten TextComment

    AdyTypewritten TextComments

    AdyTypewritten TextCharacter Literals

    AdyTypewritten Textx

    AdyTypewritten Text

    AdyTypewritten Textx

    AdyTypewritten TextContains a space

    AdyTypewritten Textx

    AdyTypewritten TextMore than 30 characters

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextContains a space

    AdyTypewritten Text

    AdyTypewritten Text

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    3. Identify the reserved words in the following list.

    Word Reserved? Y/N

    create

    make

    table

    seat

    alter

    rename

    row

    number

    web

    4. What kind of lexical unit (for example Reserved word, Delimiter, Literal, Comment) is each

    of the following?

    Value Lexical Unit

    SELECT

    :=

    'TEST'

    FALSE

    -- new process

    FROM

    /*select the country with the highest elevation */

    V_test

    4.09

    AdyTypewritten Texty

    AdyTypewritten Texty

    AdyTypewritten Text

    AdyTypewritten Textn

    AdyTypewritten Textn

    AdyTypewritten Text

    AdyTypewritten Texty

    AdyTypewritten Texty

    AdyTypewritten Texty

    AdyTypewritten Texty

    AdyTypewritten Textn

    AdyTypewritten Text

    AdyTypewritten Textreserved word

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Textdelimiter

    AdyTypewritten Textcomment

    AdyTypewritten Textreserved word

    AdyTypewritten Textcomment

    AdyTypewritten Text

    AdyTypewritten Textidentifier

    AdyTypewritten Textliteral

    AdyTypewritten Text

    AdyTypewritten Textliteral

    AdyTypewritten Textboolean literals

    AdyTypewritten Text

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 3: Recognizing Data Types

    Vocabulary

    Identify the vocabulary word for each definition below:

    Store large blocks of single-byte or fixed width multi-byte NCHAR data in the database.

    Hold values, called locators, that specify the location of large objects (such as graphic images) that are stored out of line.

    Hold a single value with no internal components.

    Store large unstructured or structured binary objects.

    Contain internal elements that are either scalar (record) or composite (record and table)

    Store large binary files outside of the database.

    Hold values, called pointers, that point to a storage location.

    A schema object with a name, attributes, and methods.

    Store large blocks of character data in the database.

    Try It / Solve It

    1. In your own words, describe what a data type is and explain why it is important.

    AdyTypewritten TextScalar

    AdyTypewritten TextComposite

    AdyTypewritten TextLarge Object (LOB)

    AdyTypewritten TextReference

    AdyTypewritten TextBinary file

    AdyTypewritten TextNational language character large object

    AdyTypewritten TextCharacter large object

    AdyTypewritten TextBinary large object

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextA data type specifies a storage format, constraints, and a valid range of values.

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten TextObject

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    2. Match the data type category (LOB, Scalar, Composite, Reference, and Object) with the

    appropriate definition. Each data type may be used more than once.

    Description Data Type

    Stores a large amount of data

    Has internal components that can be manipulated individually

    Has a name, attributes, and methods

    Includes CLOBs, BLOBs, BFILEs, and NCLOBs

    Has no internal components

    Includes TABLEs, RECORDs, NESTED TABLEs, and VARRAYs

    Includes TIMESTAMP, DATE, BINARY_INTEGER, LONG, LONG RAW, and BOOLEAN

    Holds values, called pointers, that point to a storage location

    3. Enter the data type category for each value into the Data Type Category column. In

    the Data Type column, enter a specific data type that can be used for the value. The first one has been done for you.

    Value Data Type Category

    Data Type

    Switzerland Scalar VARCHAR2

    100.20

    1053

    12-DEC-2005

    False

    Index Last_name

    1 'Newman'

    2 'Raman'

    3 'Han'

    A movie

    A soundbyte

    A picture

    AdyTypewritten TextLOB

    AdyTypewritten Textcomposite

    AdyTypewritten Text

    AdyTypewritten Textobject

    AdyTypewritten Text

    AdyTypewritten TextLOB

    AdyTypewritten Textscalar

    AdyTypewritten Textcomposite

    AdyTypewritten Textscalar

    AdyTypewritten Textreference

    AdyTypewritten Textnumber

    AdyTypewritten Textnumber

    AdyTypewritten Textnumber

    AdyTypewritten TextPLS_INTEGER

    AdyTypewritten Textinterval year to month

    AdyTypewritten Text

    AdyTypewritten Textboolean

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Textdate

    AdyTypewritten Textcharacter

    AdyTypewritten Textlong raw

    AdyTypewritten Textcharacter

    AdyTypewritten Textchar

    AdyTypewritten Textcharacter

    AdyTypewritten Textchar

    AdyTypewritten Textcharacter

    AdyTypewritten Textchar

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 4: Using Scalar Data Types

    Vocabulary

    Identify the vocabulary word for each definition below:

    A datatype that stores one of the three possible values used for logical calculations: TRUE, FALSE, or NULL.

    Attribute used to declare a variable according to another previously declared variable or database column.

    Try It / Solve It

    1. Declarations: A. Which of the following variable declarations are valid?

    Declaration Valid or Invalid

    a number_of_students PLS_INTEGER;

    b STUDENT_NAME VARCHAR2(10)=Johnson;

    c stu_per_class CONSTANT NUMBER;

    d tomorrow DATE := SYSDATE+1;

    B. For those declarations in 1.A. that are invalid, describe why they are invalid.

    C. Write an anonymous block in which you declare and print each of the variables in 1.A,

    correcting the invalid declarations. 2. Evaluate the variables in the following code. Answer the following questions about each

    variable. Is it named well? Why or why not? If it is not named well, what would be a better name and why? DECLARE country_name VARCHAR2 (50);

    median_age NUMBER(6,2); BEGIN SELECT country_name, median_age INTO country_name, median_age

    FROM wf_countries WHERE country_name = 'United States of America';

    DBMS_OUTPUT.PUT_LINE(' The median age in '||country_name||' is '||median_age||'.'); END;

    3. Examine the declarations in question 2. Change the declarations so that they use the

    %TYPE attribute.

    AdyTypewritten TextBoolean

    AdyTypewritten Text

    AdyTypewritten Text

    AdyTypewritten Text%TYPE

    AdyTypewritten Textvalid

    AdyTypewritten Textinvalid

    AdyTypewritten Textvalid

    AdyTypewritten Textinvalid

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    4. In your own words, describe why using the %TYPE attribute is better than hard-coding data types. Can you explain how you could run into problems in the future by hard-coding the data types of the country_name and median_age variables in question 2?

    5. Create the following anonymous block:

    BEGIN DBMS_OUTPUT.PUT_LINE('Hello World'); END;

    A. Add a declarative section to this PL/SQL block. In the declarative section, declare the

    following variables:

    A variable named TODAY of datatype DATE. Initialize TODAY with SYSDATE.

    A variable named TOMORROW with the same datatype as TODAY. Use the %TYPE attribute to declare this variable.

    B. In the executable section, initialize the TOMORROW variable with an expression that

    calculates tomorrows date (add 1 to the value in TODAY). Print the value of TODAY and TOMORROW after printing Hello World.

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 5: Writing PL/SQL Executable Statements

    Vocabulary

    Identify the vocabulary word for each definition below:

    Converts values from one data type to another by using built-in functions.

    Converts data types dynamically if they are mixed in a statement.

    Try It / Solve It

    1. Examine the following code and then answer the questions. DECLARE x VARCHAR2(20); BEGIN x:= '123' + '456' ; DBMS_OUTPUT.PUT_LINE(x); END;

    A. What do you think the output will be when you run the above code? B. Now, run the code. What is the output? C. In your own words, describe what happened when you ran the code. Did any implicit

    conversions take place?

    2. Write an anonymous PL/SQL block that assigns the programmers full name to a variable, and then displays the number of characters in the name.

    3. Write an anonymous PL/SQL block that uses today's date and outputs it in the format of

    Month dd, yyyy. Store the date in a DATE variable called my_date. Create another variable of the DATE type called v_last_day. Assign the last day of this month to v_last_day. Display the value of v_last_day.

    4. Modify the program created in question 3 to add 45 days to todays date and then

    calculate and display the number of months between the two dates.

    AdyTypewritten TextImplicit Conversions

    AdyTypewritten TextExplicit Conversions

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    5. Examine the following code and then answer the questions.

    DECLARE x NUMBER(6); BEGIN x := 5 + 3 * 2 ; DBMS_OUTPUT.PUT_LINE(x); END;

    A. What do you think the output will be when you run the above code?

    B. Now run the code. What is the output?

    C. In your own words, explain the results.

    6. Examine the following code and then answer the question.

    DECLARE v_number NUMBER; v_boolean BOOLEAN; BEGIN v_number := 25; v_boolean := NOT(v_number > 30); END; What value is assigned to v_boolean?

    7. List two drawbacks to relying on implicit data type conversions.

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    Section 2 Lesson 6: Nested Blocks and Variable Scope

    Vocabulary

    Identify the vocabulary word for each definition below.

    A label given to a block.

    Consists of all the blocks in which the variable is either local (the

    declaring block) or global (nested blocks within the declaring block) .

    The portion of the program where the variable can be accessed without

    using a qualifier.

    Try It / Solve It

    1. Evaluate the PL/SQL block below and determine the value of each of the following variables according to the rules of scoping.

    DECLARE

    weight NUMBER(3) := 600;

    message VARCHAR2(255) := 'Product 10012';

    BEGIN

    DECLARE

    weight NUMBER(3) := 1;

    message VARCHAR2(255) := 'Product 11001';

    new_locn VARCHAR2(50) := 'Europe';

    BEGIN

    weight := weight + 1;

    new_locn := 'Western ' || new_locn;

    -- Position 1 --

    END;

    weight := weight + 1;

    message := message || ' is in stock';

    -- Position 2 --

    END;

    A. The value of weight at position 1 is:

    B. The value of new_locn at position 1 is:

    C. The value of weight at position 2 is:

    D. The value of message at position 2 is:

    E. The value of new_locn at position 2 is:

    AdyTypewritten TextQualifying an Identifier

    AdyTypewritten TextLocal and Global Variables

    AdyTypewritten TextVariable Visibility

    AdyTypewritten Text

  • Copyright 2013, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their

    respective owners.

    2. Enter and run the following PL/SQL block, which contains a nested block. Look at the output and answer the questions.

    DECLARE

    v_employee_id employees.employee_id%TYPE;

    v_job employees.job_id%TYPE;

    BEGIN

    SELECT employee_id, job_id INTO v_employee_id, v_job

    FROM employees

    WHERE employee_id = 100;

    DECLARE

    v_employee_id employees.employee_id%TYPE;

    v_job employees.job_id%TYPE;

    BEGIN

    SELECT employee_id, job_id INTO v_employee_id, v_job

    FROM employees

    WHERE employee_id = 103;

    DBMS_OUTPUT.PUT_LINE(v_employee_id|| ' is a '||v_job);

    END;

    DBMS_OUTPUT.PUT_LINE(v_employee_id|| ' is a '||v_job);

    END;

    A. Why does the inner block display the job_id of employee 103, not employee 100?

    B. Why does the outer block display the job_id of employee 100, not employee 103?

    C. Modify the code to display the details of employee 100 in the inner block. Use block labels.

    PLSQL_s01_l01_try.pdfPLSQL_s01_l02_try.pdfPLSQL_s01_l03_try.pdfPLSQL_s02_l01_try.pdfPLSQL_s02_l02_try.pdfPLSQL_s02_l03_try.pdfPLSQL_s02_l04_try.pdfPLSQL_s02_l05_try.pdfPLSQL_s02_l06_try.pdf