Tài liệu developing a simple PL / SQL docx

56 405 1
Tài liệu developing a simple PL / SQL docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Developing a Simple PL/SQL Block 21 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć2 Schedule: Timing Topic 60 minutes Lecture 30 minutes Practice 90 minutes Total Developing a Simple PL/SQL Block 21Ć3 Objectives In this lesson, you create a simple PL/SQL block after learning the various elements that compose a block. At the end of this lesson, you should be able to D Declare and use variables and constants in PL/SQL. D Assign new values to variables within the executable section. D Create and execute a named PL/SQL subprogram in Procedure Builder. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć4 Developing a Simple PL/SQL Block 21Ć5 Overview A PL/SQL block is comprised of up to three sections: declarative (optional), executable (required), and exception handling (optional). Only BEGIN and END keywords are required. Each subprogram contains an additional section, the header (required). You can store and change values within a PL/SQL block by declaring and referencing variables and other identifiers. Handling Variables D Declare and initialize variables within the declaration section. D Assign new values to variables within the executable section. D Pass values into PL/SQL blocks through parameters. D View the results from a PL/SQL block through output variables. Note: The END keyword can be optionally followed by the name of the subprogram for clarity. Class Management Note: PowerPoint: The bottom slide contains the build feature. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć6 Developing a Simple PL/SQL Block 21Ć7 Declaring PL/SQL Variables and Constants You need to declare all identifiers within the declaration section before referencing them within the PL/SQL block. Syntax identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; where: identifier is the name of the identifier. CONSTANT constrains the identifier so that its value cannot change; constants must be initialized. datatype is a scalar or composite datatype. NOT NULL constrains the variable so that it must contain a value; NOT NULL variables must be initialized. expr is any PL/SQL expression that can be a literal, another variable, or an expression involving operators and functions. Guidelines D Name the identifier according to the same rules used for SQL objects. D You can use naming conventions, for example v_name to represent a variable, and c_name to represent a constant. D You have the option of assigning an initial value to variables, unless they are NOT NULL. D Initialize the variable to an expression with the assignment operator (:=), or, equivalently, with the DEFAULT reserved word; otherwise, variables are initialized to NULL by default. D Declare at most one identifier per line. For more information, see PL/SQL User’s Guide and Reference, Release 2.3, “Datatypes.” Technical Note: Identifiers must not be longer than 30 characters. The first character must be a letter, the remaining characters may be letters, numbers, or special symbols. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć8 Class Management Note: Mention that the NUMBER, CHAR, and VARCHAR2 datatypes have subtypes. Additional base types are RAW, ROWID, and MLSLABEL (Trusted Oracle). Developing a Simple PL/SQL Block 21Ć9 Declaring Scalar Variables PL/SQL supports three datatypes—scalar, composite, and reference—that you can use for declaring variables, constants, and pointers. Scalar Datatypes A scalar datatype holds a single value and has no internal components. Scalar datatypes can be classified into four categories: number, character, date and time, or Boolean. Character and number datatypes have subtypes that associate a base type to a constraint. For example, INTEGER and POSITIVE are subtypes of the NUMBER base type. Datatype Description BINARY_INTEGER Base type for integers between –2147483647 and 2147483647. NUMBER [(precision,scale)] Base type for fixed and floating point numbers. CHAR [(maximum_length)] Base type for fixed length character data up to 32767 bytes. If you do not specify a maximum_length, the default length is set to 1. LONG Base type for variable length character data up to 32760 bytes. LONG RAW Base type for binary data up to 32760 bytes. VARCHAR2(maximum_length) Base type for variable length character data up to 32767 bytes. DATE Base type for dates and times. BOOLEAN Base type that stores one of three possible values used for logical calculations: TRUE, FALSE, or NULL. Note: The above list is abridged. For the complete list, see the PL/SQL User’s Guide and Reference, Release 2.3, “Datatypes.” Technical Note: Reference types are a third type used with variables. It is not covered in this course. The LONG datatype is similar to VARCHAR2, except the maximum length of a LONG value is 32,760 bytes. Therefore, values longer than 32,760 bytes cannot be selected from a LONG database column into a LONG PL/SQL variable. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć10 Class Management Note: CMN for page 21-12. The %TYPE attribute has some overhead involved in that a SELECT statement is issued against the database to obtain the datatype. If the PL/SQL code is in a client tool, the SELECT must be executed each time the block is executed. If the PL/SQL code is a stored procedure, the column or definition is stored as part of the P-code. However, if the table definition changes, then a recompile is forced. [...]... value to the variable Developing a Simple PL/ SQL Block 21Ć13 21Ć14 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring Composite Datatypes A composite datatype contains internal components and is reusable Two types of composite datatypes are available in PL/ SQL: TABLE and RECORD PL/ SQL Table D A PL/ SQL TABLE datatype is not the same as a database table D A PL/ SQL TABLE is similar... to a one-dimensional array D A PL/ SQL TABLE must contain two components: D D D A primary key of datatype BINARY_INTEGER that indexes the PL/ SQL TABLE A column of a scalar datatype, which stores the PL/ SQL TABLE elements A PL/ SQL TABLE can increase dynamically because it is unconstrained Technical Note: PL/ SQL TABLES in PL/ SQL version 2.3 can be created as a TABLE of RECORDS In the meantime, create a. .. being assigned to the PL/ SQL TABLE of that type Do not initialize the PL/ SQL TABLE Class Management Note: PowerPoint: The top slide uses the build feature Developing a Simple PL/ SQL Block 21Ć17 21Ć18 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring Composite Datatypes continued PL/ SQL RECORD D A PL/ SQL RECORD datatype is not the same as a row in a database table D A PL/ SQL RECORD... the board the structure of DEPT_RECORD Developing a Simple PL/ SQL Block 21Ć23 21Ć24 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder PL/ SQL Block Syntax Rules Because PL/ SQL is an extension of SQL, the general syntax rules that apply to SQL are also applicable to the PL/ SQL language Guidelines D Identifiers can contain up to 30 characters, but they must start with an alphabetic character... binary integer value that is the index of the PL/ SQL TABLE and could also be a variable plsql_record_name is the name of the PL/ SQL RECORD field_name is the field from the PL/ SQL RECORD expr can be a variable, constant, literal, or function call, but not a database column Developing a Simple PL/ SQL Block 21Ć29 21Ć30 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Assigning Values to Variables... RECORD are PL/ SQL TABLE types Developing a Simple PL/ SQL Block 21Ć15 21Ć16 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring Composite Datatypes continued Declaring PL/ SQL Tables 1 Declare a TABLE datatype 2 Declare a variable of that datatype Syntax TYPE type_name IS TABLE OF scalar_datatype [NOT NULL] INDEX BY BINARY_INTEGER; identifier type_name; where: type_name is the name...Declaring Scalar Variables continued Examples Declare a variable to store the gender code (M or F) v_gender CHAR(1); Declare a variable to count the iterations of a loop and initialize the variable to 0 v_count BINARY_INTEGER := 0; Declare a variable to accumulate the total salary for a department and initialize the variable to 0 v_total_sal NUMBER(9,2) := 0; Declare a variable to store the ship date... Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring Scalar Variables continued The %TYPE Attribute When you declare PL/ SQL variables to hold column values, you must ensure that the variable is of the correct datatype and precision If it is not, then a PL/ SQL error will occur during execution Rather than hard-coding the datatype and precision of a variable, you can declare a variable according... so the variable is always compatible with the database column or identifier used to populate the variable Advantages of Using the %TYPE Attribute D D The datatype of the underlying database column may be unknown The datatype of the underlying database column may change at runtime Examples Declare variables to store the first and last names for an employee v_last_name v_first_name s_emp.last_name%TYPE;... similar in structure to a record in a 3GL D A PL/ SQL RECORD must contain one or more components of any scalar, RECORD, or PL/ SQL TABLE datatype called fields These uniquely named fields can have different datatypes D The PL/ SQL RECORD allows you to treat this collection of fields as one logical unit D PL/ SQL RECORDS are convenient for fetching a row of data from a table for processing in a PL/ SQL block . PL/ SQL: TABLE and RECORD. PL/ SQL Table D A PL/ SQL TABLE datatype is not the same as a database table. D A PL/ SQL TABLE is similar to a one-dimensional array declaring variables, constants, and pointers. Scalar Datatypes A scalar datatype holds a single value and has no internal components. Scalar datatypes can

Ngày đăng: 10/12/2013, 17:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan