Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright  Oracle Corporation, 1998. All rights reserved. 16 Declaring Variables.

Similar presentations


Presentation on theme: "Copyright  Oracle Corporation, 1998. All rights reserved. 16 Declaring Variables."— Presentation transcript:

1

2 Copyright  Oracle Corporation, 1998. All rights reserved. 16 Declaring Variables

3 16-2 Copyright  Oracle Corporation, 1998. All rights reserved. Objectives After completing this lesson, you should be able to do the following: List the benefits of PL/SQL Recognize the basic PL/SQL block and its sections Describe the significance of variables in PL/SQL Declare PL/SQL variables Execute a PL/SQL block After completing this lesson, you should be able to do the following: List the benefits of PL/SQL Recognize the basic PL/SQL block and its sections Describe the significance of variables in PL/SQL Declare PL/SQL variables Execute a PL/SQL block

4 16-3 Copyright  Oracle Corporation, 1998. All rights reserved. About PL/SQL 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. 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.

5 16-4 Copyright  Oracle Corporation, 1998. All rights reserved. Benefits of PL/SQL IntegrationIntegration Application Oracle Server Sharedlibrary

6 16-5 Copyright  Oracle Corporation, 1998. All rights reserved. Benefits of PL/SQL Application Other DBMSs Application Oracle with PL/SQL SQL SQL SQL SQL SQLIF...THENSQLELSESQL END IF; SQL Improved Performance

7 16-6 Copyright  Oracle Corporation, 1998. All rights reserved. Benefits of PL/SQL Modularize program development DECLARE BEGIN EXCEPTION END;

8 16-7 Copyright  Oracle Corporation, 1998. All rights reserved. Benefits of PL/SQL It is portable. You can declare identifiers. It is portable. You can declare identifiers.

9 16-8 Copyright  Oracle Corporation, 1998. All rights reserved. Benefits of PL/SQL You can program with procedural language control structures. It can handle errors. You can program with procedural language control structures. It can handle errors.

10 16-9 Copyright  Oracle Corporation, 1998. All rights reserved. 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;

11 16-10 Copyright  Oracle Corporation, 1998. All rights reserved. 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;

12 16-11 Copyright  Oracle Corporation, 1998. All rights reserved. Block Types 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;

13 16-12 Copyright  Oracle Corporation, 1998. All rights reserved. Program Constructs Anonymous block Application trigger Stored procedure/ function Database trigger Application procedure/ function Packaged procedure/ function function DECLARE BEGIN EXCEPTION END;

14 16-13 Copyright  Oracle Corporation, 1998. All rights reserved. Use of Variables Use variables for: Temporary storage of data Manipulation of stored values Reusability Ease of maintenance Use variables for: Temporary storage of data Manipulation of stored values Reusability Ease of maintenance

15 16-14 Copyright  Oracle Corporation, 1998. All rights reserved. Handling Variables in PL/SQL Declare and initialize variables in the declaration section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. View results through output variables. Declare and initialize variables in the declaration section. Assign new values to variables in the executable section. Pass values into PL/SQL blocks through parameters. View results through output variables.

16 16-15 Copyright  Oracle Corporation, 1998. All rights reserved. Types of Variables PL/SQL variables: – Scalar – Composite – Reference – LOB (large objects) Non-PL/SQL variables: Bind and host variables PL/SQL variables: – Scalar – Composite – Reference – LOB (large objects) Non-PL/SQL variables: Bind and host variables

17 16-16 Copyright  Oracle Corporation, 1998. All rights reserved. Types of Variables PL/SQL variables: – Scalar – Composite – Reference – LOB (large objects) Non-PL/SQL variables: Bind and host variables PL/SQL variables: – Scalar – Composite – Reference – LOB (large objects) Non-PL/SQL variables: Bind and host variables

18 16-17 Copyright  Oracle Corporation, 1998. All rights reserved. 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

19 16-18 Copyright  Oracle Corporation, 1998. All rights reserved. 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 16-19 Copyright  Oracle Corporation, 1998. All rights reserved. Declaring PL/SQL Variables Guidelines Follow naming conventions. Initialize variables designated as NOT NULL. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Declare at most one identifier per line.Guidelines Follow naming conventions. Initialize variables designated as NOT NULL. Initialize identifiers by using the assignment operator (:=) or the DEFAULT reserved word. Declare at most one identifier per line.

21 16-20 Copyright  Oracle Corporation, 1998. All rights reserved. Naming Rules 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. 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. 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 16-21 Copyright  Oracle Corporation, 1998. All rights reserved. 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;

23 16-22 Copyright  Oracle Corporation, 1998. All rights reserved. Variable Initialization and Keywords Using: Assignment operator (:=) DEFAULT keyword NOT NULL constraintUsing: Assignment operator (:=) DEFAULT keyword NOT NULL constraint

24 16-23 Copyright  Oracle Corporation, 1998. All rights reserved. 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 “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.” TRUE 256120.08 256120.08

25 16-24 Copyright  Oracle Corporation, 1998. All rights reserved. Base Scalar DatatypesScalar Base Scalar DatatypesScalar VARCHAR2 (maximum_length) NUMBER [(precision, scale)] DATE CHAR [(maximum_length)] LONG LONG RAW BOOLEAN BINARY_INTEGER PLS_INTEGER VARCHAR2 (maximum_length) NUMBER [(precision, scale)] DATE CHAR [(maximum_length)] LONG LONG RAW BOOLEAN BINARY_INTEGER PLS_INTEGER

26 16-25 Copyright  Oracle Corporation, 1998. All rights reserved. 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; ExamplesExamples

27 16-26 Copyright  Oracle Corporation, 1998. All rights reserved. The %TYPE Attribute Declare a variable according to: – A database column definition – Another previously declared variable Prefix %TYPE with: – The database table and column – The previously declared variable name Declare a variable according to: – A database column definition – Another previously declared variable Prefix %TYPE with: – The database table and column – The previously declared variable name

28 16-27 Copyright  Oracle Corporation, 1998. All rights reserved. Declaring Variables with the %TYPE Attribute ExamplesExamples... 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;...

29 16-28 Copyright  Oracle Corporation, 1998. All rights reserved. Declaring Boolean Variables 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 always yield TRUE, FALSE, or NULL. 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. The variables are connected by the logical operators AND, OR, and NOT. The variables always yield TRUE, FALSE, or NULL. Arithmetic, character, and date expressions can be used to return a Boolean value.

30 16-29 Copyright  Oracle Corporation, 1998. All rights reserved. Composite Datatypes Have internal components that can be manipulated individually PL/SQL TABLES PL/SQL RECORDS NESTED TABLE VARRAY types Have internal components that can be manipulated individually PL/SQL TABLES PL/SQL RECORDS NESTED TABLE VARRAY types

31 16-30 Copyright  Oracle Corporation, 1998. All rights reserved. LOB Datatype Variables Recipe(CLOB) Photo(BLOB) Movie(BFILE) NCLOB

32 16-31 Copyright  Oracle Corporation, 1998. All rights reserved. Bind Variables Server O/S Bind Variable

33 16-32 Copyright  Oracle Corporation, 1998. All rights reserved. Referencing Non-PL/SQL Variables Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). Store the annual salary into a SQL*Plus host variable. Reference non-PL/SQL variables as host variables. Prefix the references with a colon (:). :g_monthly_sal := v_sal / 12;

34 16-33 Copyright  Oracle Corporation, 1998. All rights reserved. Example: VARIABLE g_salary NUMBER BEGIN SELECT salary INTO :g_salary FROM emp WHERE emp_id = 178; END;/ PRINT g_salary Example: VARIABLE g_salary NUMBER BEGIN SELECT salary INTO :g_salary FROM emp WHERE emp_id = 178; END;/ PRINT g_salary

35 16-34 Copyright  Oracle Corporation, 1998. All rights reserved. Example SET SERVEROUTPUT ON DEFINE p_annual_sal = 60000 DECLARE v_salNUMBER(9,2) := &p_annual_sal; BEGIN v_sal := v_sal/12; DBMS_OUTPUT.PUT_LINE (‘The monthly salary is ‘||TO_CHAR(v_sal)); END;/ SET SERVEROUTPUT ON DEFINE p_annual_sal = 60000 DECLARE v_salNUMBER(9,2) := &p_annual_sal; BEGIN v_sal := v_sal/12; DBMS_OUTPUT.PUT_LINE (‘The monthly salary is ‘||TO_CHAR(v_sal)); END;/

36 16-35 Copyright  Oracle Corporation, 1998. All rights reserved. DBMS_OUTPUT.PUT_LINE An Oracle-supplied packaged procedure An alternative for displaying data from a PL/SQL block Must be enabled in SQL*Plus with SET SERVEROUTPUT ON SET SERVEROUTPUT ON An Oracle-supplied packaged procedure An alternative for displaying data from a PL/SQL block Must be enabled in SQL*Plus with SET SERVEROUTPUT ON SET SERVEROUTPUT ON

37 16-36 Copyright  Oracle Corporation, 1998. All rights reserved. Summary PL/SQL blocks are composed of the following sections: – Declarative (optional) – Executable (required) – Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. PL/SQL blocks are composed of the following sections: – Declarative (optional) – Executable (required) – Exception handling (optional) A PL/SQL block can be an anonymous block, procedure, or function. DECLARE BEGIN EXCEPTION END;

38 16-37 Copyright  Oracle Corporation, 1998. All rights reserved. Summary PL/SQL identifiers: – Are defined in the declarative section – Can be of scalar, composite, reference, or LOB datatype – Can be based on the structure of another variable or database object – Can be initialized PL/SQL identifiers: – Are defined in the declarative section – Can be of scalar, composite, reference, or LOB datatype – Can be based on the structure of another variable or database object – Can be initialized

39 16-38 Copyright  Oracle Corporation, 1998. All rights reserved. Practice Overview Determining validity of declarations Developing a simple PL/SQL block Determining validity of declarations Developing a simple PL/SQL block

40 16-39 Copyright  Oracle Corporation, 1998. All rights reserved.

41 16-40 Copyright  Oracle Corporation, 1998. All rights reserved.

42 16-41 Copyright  Oracle Corporation, 1998. All rights reserved.

43 16-42 Copyright  Oracle Corporation, 1998. All rights reserved.


Download ppt "Copyright  Oracle Corporation, 1998. All rights reserved. 16 Declaring Variables."

Similar presentations


Ads by Google