Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.

Similar presentations


Presentation on theme: "1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements."— Presentation transcript:

1

2 1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements Writing Executable Statements Writing Executable Statements Interacting with the Oracle Server Interacting with the Oracle Server Interacting with the Oracle Server Interacting with the Oracle Server Writing Control Structures Writing Control Structures Writing Control Structures Writing Control Structures Working with Composite Datatypes Working with Composite Datatypes Working with Composite Datatypes Working with Composite Datatypes Cursors Cursors Cursors Handling Exceptions Handling Exceptions Handling Exceptions Handling Exceptions Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements Writing Executable Statements Writing Executable Statements Interacting with the Oracle Server Interacting with the Oracle Server Interacting with the Oracle Server Interacting with the Oracle Server Writing Control Structures Writing Control Structures Writing Control Structures Writing Control Structures Working with Composite Datatypes Working with Composite Datatypes Working with Composite Datatypes Working with Composite Datatypes Cursors Cursors Cursors Handling Exceptions Handling Exceptions Handling Exceptions Handling Exceptions

3 2 ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: List the benefits of PL/SQL List the benefits of PL/SQL Recognize the basic PL/SQL block and its sections Recognize the basic PL/SQL block and its sections Describe the significance of variables in PL/SQL Describe the significance of variables in PL/SQL Declare PL/SQL variables Declare PL/SQL variables Execute a PL/SQL block Execute a PL/SQL block After completing this lesson, you should be able to do the following: After completing this lesson, you should be able to do the following: List the benefits of PL/SQL List the benefits of PL/SQL Recognize the basic PL/SQL block and its sections Recognize the basic PL/SQL block and its sections Describe the significance of variables in PL/SQL Describe the significance of variables in PL/SQL Declare PL/SQL variables Declare PL/SQL variables Execute a PL/SQL block Execute a PL/SQL block

4 3 About PL/SQL PL/SQL is an extension to SQL with design features of programming languages. PL/SQL is an extension to SQL with design features of programming languages. Data manipulation and query statements of SQL are included within procedural units of code. Data manipulation and query statements of SQL are included within procedural units of code. PL/SQL not case sensitive language PL/SQL not case sensitive language A semicolon ; must end each PL/SQL command A semicolon ; must end each PL/SQL command PL/SQL is an extension to SQL with design features of programming languages. PL/SQL is an extension to SQL with design features of programming languages. Data manipulation and query statements of SQL are included within procedural units of code. Data manipulation and query statements of SQL are included within procedural units of code. PL/SQL not case sensitive language PL/SQL not case sensitive language A semicolon ; must end each PL/SQL command A semicolon ; must end each PL/SQL command

5 4 Benefits of PL/SQL Integration Integration Application Oracle Server Sharedlibrary

6 5 Benefits of PL/SQL Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL Improve performance Improve performance

7 6 PL/SQL Block Structure DECLARE – Optional Variables, cursors, user-defined exceptions BEGIN – Mandatory – SQL statements – PL/SQL statements EXCEPTION – Optional Actions to perform when errors occur END; – Mandatory DECLARE – Optional Variables, cursors, user-defined exceptions BEGIN – Mandatory – SQL statements – PL/SQL statements EXCEPTION – Optional Actions to perform when errors occur END; – Mandatory DECLARE BEGIN EXCEPTION END;

8 7 PL/SQL Block Structure DECLARE v_variable VARCHAR2(5); BEGIN SELECTcolumn_name INTOv_variable FROMtable_name; EXCEPTION WHEN exception_name THEN... END; DECLARE v_variable VARCHAR2(5); BEGIN SELECTcolumn_name INTOv_variable FROMtable_name; EXCEPTION WHEN exception_name THEN... END; DECLARE BEGIN EXCEPTION END;

9 8 Block Types AnonymousProcedureFunction AnonymousProcedureFunction [DECLARE]BEGIN --statements --statements[EXCEPTION]END;[DECLARE]BEGIN [EXCEPTION]END; PROCEDURE name ISBEGIN --statements --statements[EXCEPTION]END; PROCEDURE name ISBEGIN --statements --statements[EXCEPTION]END; FUNCTION name RETURN datatype ISBEGIN --statements --statements RETURN value; RETURN value;[EXCEPTION]END; FUNCTION name RETURN datatype ISBEGIN --statements --statements RETURN value; RETURN value;[EXCEPTION]END; These blocks can be entirely separate or nested one within another. Anonymous blocks are unnamed blocks. Subprograms are named PL/SQL blocks that can take parameters and can be invoked. These blocks can be entirely separate or nested one within another. Anonymous blocks are unnamed blocks. Subprograms are named PL/SQL blocks that can take parameters and can be invoked.

10 9 Program Constructs Anonymous block Application trigger Stored procedure/ function Database trigger Application procedure/ function Packaged procedure/ function function DECLARE BEGIN EXCEPTION END;

11 10 Declaring Variables

12 11 Use of Variables Use variables for: Use variables for: Temporary storage of data Temporary storage of data Manipulation of stored values Manipulation of stored values Reusability Reusability Ease of maintenance Ease of maintenance Use variables for: Use variables for: Temporary storage of data Temporary storage of data Manipulation of stored values Manipulation of stored values Reusability Reusability Ease of maintenance Ease of maintenance

13 12 Handling Variables in PL/SQL Declare and initialize variables in the declaration section. Declare and initialize variables in the declaration section. When you declare variable, the variables default value is always NULL When you declare variable, the variables default value is always NULL Assign new values to variables in the executable section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. Pass values into PL/SQL blocks through parameters. View results through output variables. View results through output variables. Declare and initialize variables in the declaration section. Declare and initialize variables in the declaration section. When you declare variable, the variables default value is always NULL When you declare variable, the variables default value is always NULL Assign new values to variables in the executable section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. Pass values into PL/SQL blocks through parameters. View results through output variables. View results through output variables.

14 13 TRUE Types of Variables 25-OCT-99 25-OCT-99 Atlanta “ Four score and seven years ago our fathers brought forth upon this continent, a new nation, conceived in LIBERTY, and dedicated to the proposition that all men are created equal. ” 256120.08 256120.08

15 14 Types of Variables The slide illustrates the following variable datatypes: The slide illustrates the following variable datatypes: TRUE represents a Boolean value. TRUE represents a Boolean value. 25-OCT-99 represents a DATE. 25-OCT-99 represents a DATE. The photograph represents a BLOB. The photograph represents a BLOB. The text of a speech represents a LONG RAW. The text of a speech represents a LONG RAW. 256120.08 represents a NUMBER datatype with precision and scale. 256120.08 represents a NUMBER datatype with precision and scale. The movie represents a BFILE. The movie represents a BFILE. The city name represents a VARCHAR2. The city name represents a VARCHAR2.

16 15 Types of Variables PL/SQL variables: PL/SQL variables: Scalar Scalar Composite Composite Reference Reference LOB (large objects) LOB (large objects) Non-PL/SQL variables: Bind and host variables Non-PL/SQL variables: Bind and host variables PL/SQL variables: PL/SQL variables: Scalar Scalar Composite Composite Reference Reference LOB (large objects) LOB (large objects) Non-PL/SQL variables: Bind and host variables Non-PL/SQL variables: Bind and host variables

17 16 Types of Variables - scalar All PL/SQL variables have a datatype, which specifies a storage format, constraints, and valid range of values. All PL/SQL variables have a datatype, which specifies a storage format, constraints, and valid range of values. Scalar datatypes hold a single value. Scalar datatypes hold a single value. The main datatypes are those that correspond to column types. The main datatypes are those that correspond to column types. PL/SQL also supports Boolean variables. PL/SQL also supports Boolean variables. All PL/SQL variables have a datatype, which specifies a storage format, constraints, and valid range of values. All PL/SQL variables have a datatype, which specifies a storage format, constraints, and valid range of values. Scalar datatypes hold a single value. Scalar datatypes hold a single value. The main datatypes are those that correspond to column types. The main datatypes are those that correspond to column types. PL/SQL also supports Boolean variables. PL/SQL also supports Boolean variables.

18 17 Types of Variables - scalar

19 18 Declaring PL/SQL Variables SyntaxExamplesSyntaxExamples identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_locationVARCHAR2(13) := 'Atlanta'; c_commCONSTANT NUMBER := 1400; Declare v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_locationVARCHAR2(13) := 'Atlanta'; c_commCONSTANT NUMBER := 1400;

20 19 Declaring PL/SQL Variables Guidelines Guidelines Follow naming conventions. Follow naming conventions. Initialize variables designated as NOT NULL and CONSTANT. Initialize variables designated as NOT NULL and CONSTANT. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Declare at most one identifier per line. Declare at most one identifier per line. Guidelines Guidelines Follow naming conventions. Follow naming conventions. Initialize variables designated as NOT NULL and CONSTANT. Initialize variables designated as NOT NULL and CONSTANT. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Declare at most one identifier per line. Declare at most one identifier per line.

21 20 Naming Rules Two variables can have the same name, provided they are in different blocks. Two variables can have the same name, provided they are in different blocks. The variable name (identifier) should not be the same as the name of table columns used in the block. The variable name (identifier) should not be the same as the name of table columns used in the block. Two variables can have the same name, provided they are in different blocks. Two variables can have the same name, provided they are in different blocks. The variable name (identifier) should not be the same as the name of table columns used in the block. The variable name (identifier) should not be the same as the name of table columns used in the block. DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END; DECLARE empnoNUMBER(4); BEGIN SELECTempno INTOempno FROMemp WHERE ename = 'SMITH'; END; Adopt a naming convention for PL/SQL identifiers: for example, v_empno Adopt a naming convention for PL/SQL identifiers: for example, v_empno

22 21 Variable Initialization and Keywords Using: Using: Assignment operator (:=) Assignment operator (:=) DEFAULT keyword DEFAULT keyword NOT NULL constraint NOT NULL constraint Using: Using: Assignment operator (:=) Assignment operator (:=) DEFAULT keyword DEFAULT keyword NOT NULL constraint NOT NULL constraint

23 22 Assigning Values to Variables v_ename := 'Maduro'; v_hiredate := '31-DEC-98'; SyntaxExamples Set a predefined hiredate for new employees. SyntaxExamples Set the employee name to Maduro. identifier := expr;

24 23 Scalar Datatypes Hold a single value Have no internal components Hold a single value Have no internal components 25-OCT-99 25-OCT-99 Atlanta TRUE 256120.08 256120.08

25 24 Base Scalar Datatypes VARCHAR2 (maximum_length) VARCHAR2 (maximum_length) NUMBER [(precision, scale)] NUMBER [(precision, scale)] DATE DATE CHAR [(maximum_length)] CHAR [(maximum_length)] LONG LONG BOOLEAN BOOLEAN BINARY_INTEGER BINARY_INTEGER VARCHAR2 (maximum_length) VARCHAR2 (maximum_length) NUMBER [(precision, scale)] NUMBER [(precision, scale)] DATE DATE CHAR [(maximum_length)] CHAR [(maximum_length)] LONG LONG BOOLEAN BOOLEAN BINARY_INTEGER BINARY_INTEGER

26 25 Scalar Variable Declarations v_jobVARCHAR2(9); v_countBINARY_INTEGER := 0; v_total_salNUMBER(9,2) := 0; v_orderdateDATE := SYSDATE + 7; c_tax_rateCONSTANT NUMBER(3,2) := 8.25; v_validBOOLEAN NOT NULL := TRUE; v_jobVARCHAR2(9); v_countBINARY_INTEGER := 0; v_total_salNUMBER(9,2) := 0; v_orderdateDATE := SYSDATE + 7; c_tax_rateCONSTANT NUMBER(3,2) := 8.25; v_validBOOLEAN NOT NULL := TRUE; Examples Examples

27 26 Types of Variables – Reference Directly reference specific database column or row Directly reference specific database column or row Assume data type of associated column or row Assume data type of associated column or row %TYPE data declaration syntax: %TYPE data declaration syntax: var_name tablename.fieldname%TYPE; var_name tablename.fieldname%TYPE; %ROWTYPE data declaration syntax: %ROWTYPE data declaration syntax: variable_name tablename%ROWTYPE; variable_name tablename%ROWTYPE; Directly reference specific database column or row Directly reference specific database column or row Assume data type of associated column or row Assume data type of associated column or row %TYPE data declaration syntax: %TYPE data declaration syntax: var_name tablename.fieldname%TYPE; var_name tablename.fieldname%TYPE; %ROWTYPE data declaration syntax: %ROWTYPE data declaration syntax: variable_name tablename%ROWTYPE; variable_name tablename%ROWTYPE;

28 27 The %TYPE Attribute Declare a variable according to: Declare a variable according to: A database column definition A database column definition Another previously declared variable Another previously declared variable Prefix %TYPE with: Prefix %TYPE with: The database table and column The database table and column The previously declared variable name The previously declared variable name Declare a variable according to: Declare a variable according to: A database column definition A database column definition Another previously declared variable Another previously declared variable Prefix %TYPE with: Prefix %TYPE with: The database table and column The database table and column The previously declared variable name The previously declared variable name

29 28 Declaring Variables with the %TYPE Attribute Examples Examples... v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;... v_enameemp.ename%TYPE; v_balanceNUMBER(7,2); v_min_balancev_balance%TYPE := 10;...

30 29 Declaring Boolean Variables Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. The variables are connected by the logical operators AND, OR, and NOT. The variables are connected by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value. Arithmetic, character, and date expressions can be used to return a Boolean value. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. The variables are connected by the logical operators AND, OR, and NOT. The variables are connected by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value. Arithmetic, character, and date expressions can be used to return a Boolean value.

31 30 15000 22345 312 43456 1SMITH 2JONES 3NANCY 4TIM PL/SQL table structure BINARY_INTEGER VARCHAR2 BINARY_INTEGER NUMBER PL/SQL Record Structure TRUE 23-DEC-98ATLANTA

32 31 LOB Datatype Variables Book(CLOB) Photo(BLOB) Movie(BFILE) NCLOB

33 32 Displaying PL/SQL Program Output in SQL*Plus PL/SQL output buffer PL/SQL output buffer Memory area on database server Memory area on database server Stores program’s output values before they are displayed to user Stores program’s output values before they are displayed to user Should increase size Should increase size SET SERVEROUTPUT ON SIZE buffer_size SET SERVEROUTPUT ON SIZE buffer_size Default buffer size Default buffer size 2000 bytes 2000 bytes

34 33 Displaying PL/SQL Program Output in SQL*Plus (continued) Display program output Display program output DBMS_OUTPUT.PUT_LINE('display_te xt'); Display maximum of 255 characters of text data Display maximum of 255 characters of text data

35 34 Writing a PL/SQL Program Write PL/SQL program in Notepad or another text editor Write PL/SQL program in Notepad or another text editor Copy and paste program commands into SQL*Plus Copy and paste program commands into SQL*Plus Press Enter after last program command Press Enter after last program command Type front slash ( / ) Type front slash ( / ) Then press Enter again Then press Enter again

36 35 DBMS_OUTPUT.PUT_LINEDBMS_OUTPUT.PUT_LINE An Oracle-supplied packaged procedure An Oracle-supplied packaged procedure An alternative for displaying data from a PL/SQL block An alternative for displaying data from a PL/SQL block Must be enabled in SQL*Plus with SET SERVEROUTPUT ON Must be enabled in SQL*Plus with SET SERVEROUTPUT ON An Oracle-supplied packaged procedure An Oracle-supplied packaged procedure An alternative for displaying data from a PL/SQL block An alternative for displaying data from a PL/SQL block Must be enabled in SQL*Plus with SET SERVEROUTPUT ON Must be enabled in SQL*Plus with SET SERVEROUTPUT ON

37 36 PL/SQL Program Commands

38 37 SummarySummary PL/SQL blocks are composed of the following sections: PL/SQL blocks are composed of the following sections: Declarative (optional) Declarative (optional) Executable (required) Executable (required) Exception handling (optional) Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. A PL/SQL block can be an anonymous block, procedure, or function. PL/SQL blocks are composed of the following sections: PL/SQL blocks are composed of the following sections: Declarative (optional) Declarative (optional) Executable (required) Executable (required) Exception handling (optional) Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. A PL/SQL block can be an anonymous block, procedure, or function. DECLARE BEGIN EXCEPTION END;

39 38 SummarySummary PL/SQL identifiers: PL/SQL identifiers: Are defined in the declarative section Are defined in the declarative section Can be of scalar, composite, reference, or LOB datatype Can be of scalar, composite, reference, or LOB datatype Can be based on the structure of another variable or database object Can be based on the structure of another variable or database object Can be initialized Can be initialized PL/SQL identifiers: PL/SQL identifiers: Are defined in the declarative section Are defined in the declarative section Can be of scalar, composite, reference, or LOB datatype Can be of scalar, composite, reference, or LOB datatype Can be based on the structure of another variable or database object Can be based on the structure of another variable or database object Can be initialized Can be initialized


Download ppt "1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements."

Similar presentations


Ads by Google