Tài liệu developing a simple PL / SQl pptx

56 379 1
Tài liệu developing a simple PL / SQl pptx

Đ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 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. 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.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć8 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.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder21Ć10 [...]... 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 Developing a Simple PL/ SQL Block 21Ć15 21Ć16 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring... 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 is 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... the name of the identifier plsql_table_name is the name of the PL/ SQL TABLE primary_key_value is the 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... 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 Developing a Simple PL/ SQL Block 21Ć19 21Ć20 Introduction to Oracle: SQL and PL/ SQL Using Procedure Builder Declaring Composite Datatypes continued Declaring PL/ SQL Records 1 Declare a RECORD datatype 2 Declare a variable... the identifier Example Declare PL/ SQL TABLE variables to store the first name and last name TYPE name_table_type IS TABLE OF VARCHAR2(25) INDEX BY BINARY_INTEGER; first_name_table name_table_type; last_name_table name_table_type; The NOT NULL constraint prevents nulls from being assigned to the PL/ SQL TABLE of that type Do not initialize the PL/ SQL TABLE Developing a Simple PL/ SQL Block 21Ć17 21Ć18...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... declaration creates a record with the same field names and field datatypes as a row in a table DEPT_RECORD is a record The fields are: DEPT_RECORD.ID, DEPT_RECORD.NAME, and DEPT_RECORD.REGION_ID 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... 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 of the TABLE type scalar_datatype is the datatype of the PL/ SQL TABLE elements You can use the %TYPE attribute identifier is the name of the identifier Example... Oracle: SQL and PL/ SQL Using Procedure Builder Assigning Values to Variables continued Examples Set the maximum salary identifier V_MAX_SAL to the value of current salary identifier V_SAL v_max_sal := v_sal; Store the name “Maduro” in the index identifier of 3 in the PL/ SQL TABLE of last names last_name_table (3) := ’Maduro’; Store basic information for a new employee in a PL/ SQL RECORD emp_record.last_name . 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 Oracle: SQL and PL/ SQL Using Procedure Builder21Ć8 Developing a Simple PL/ SQL Block 21Ć9 Declaring Scalar Variables PL/ SQL supports three datatypes—scalar,

Ngày đăng: 21/12/2013, 06:17

Từ khóa liên quan

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

Tài liệu liên quan