12.07.2015 Views

Oracle SQL Developer

Oracle SQL Developer

Oracle SQL Developer

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Utilizing the Main Features of PL/<strong>SQL</strong>The DBMS_OUTPUT package is a predefined <strong>Oracle</strong> package. For information about<strong>Oracle</strong> supplied packages, see <strong>Oracle</strong> Product-Specific Packages.See Also:■■<strong>SQL</strong>*Plus User's Guide and Reference for information <strong>SQL</strong>*Pluscommands.PL/<strong>SQL</strong> Packages and Types Reference.for information about <strong>Oracle</strong>supplied packages.7.3.10 Using %ROWTYPE and %TYPE Attributes to Declare Data TypesAs part of the declaration for each PL/<strong>SQL</strong> variable, you declare its data type. Usually,this data type is one of the types shared between PL/<strong>SQL</strong> and <strong>SQL</strong>, such as NUMBER orVARCHAR2. For easier maintenance of code that interacts with the database, you canalso use the special qualifiers %ROWTYPE and %TYPE to declare variables that holdtable columns or table rows.7.3.10.1 Using the %ROWTYPE Attribute to Declare VariablesFor easier maintenance of code that interacts with the database, you can use the%ROWTYPE attribute to declare a variable that represents a row in a table. A PL/<strong>SQL</strong>record is the data type that stores the same information as a row in a table.In PL/<strong>SQL</strong>, records are used to group data. A record consists of a number of relatedfields in which data values can be stored. The record can store an entire row of dataselected from the table or fetched from a cursor or cursor variable. For information onrecords, see "Using Records" on page 7-18.Columns in a row and corresponding fields in a record have the same names and datatypes. In Example 7–10, you declare a record named emp_rec. Its fields have the samenames and data types as the columns in the employees table. You use dot notation toreference fields, such as emp_rec.last_name.In Example 7–10, SELECT is used to store row information from the employees tableinto the emp_rec record. When you execute the SELECT INTO statement, the value inthe first_name column of the employees table is assigned to the first_name fieldof emp_rec, the value in the last_name column is assigned to the last_name fieldof emp_rec, and so on.Example 7–10Using %ROWTYPE with a RecordDECLARE -- declare variables-- declare record variable that represents a row fetched from the employees tableemp_rec employees%ROWTYPE; -- declare variable with %ROWTYPE attributeBEGINSELECT * INTO emp_rec FROM EMPLOYEES WHERE employee_id = 120; -- retrieve recordDBMS_OUTPUT.PUT_LINE('Employee name: ' || emp_rec.first_name || ' '|| emp_rec.last_name); -- displayEND;/Declaring variables with %ROWTYPE has several advantages. First, you do not need toknow the exact data type of the table columns. Second, if you change the databasedefinition of any of the table columns, the data types associated with the %ROWTYPEdeclaration change accordingly at run time.See Also: PL/<strong>SQL</strong> User's Guide and Reference for information on%ROWTYPE7-10 <strong>SQL</strong> <strong>Developer</strong> Online Help

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!