PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -

Slides:



Advertisements
Similar presentations
BD05/06 PL/SQL  Introduction  Structure of a block  Variables and types  Accessing the database  Control flow  Cursors  Exceptions  Procedures.
Advertisements

PL/SQL.
PL/SQL (Procedural Language extensions to SQL) Prepared by: Manoj Kathpalia Edited by: M V Ramakrishna.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
Dr. James Dullea, CSC8490 Introduction to PL/SQLSlide 1 of 36 7From Prof. Dullea CSC8490 Introduction to PL/SQL Module 01-9 Revised: June 12, 2005 Dr.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
SQL enables us to create, organize, retrieve and maintain data stored in database it does not provide the features which a typical programming language.
EE Copyright س Oracle Corporation, All rights reserved. ® Review of PL/SQL.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
1 JumpStart PL/SQL Wang Hao. 2 Outline PL/SQL Programming SQL*Plus and SQL commands.
6 Copyright © 2009, Oracle. All rights reserved. Working with Composite Data Types.
Oracle10g Developer: PL/SQL Programming1 Objectives Programming fundamentals The PL/SQL block Define and declare variables Initialize variables The NOT.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
L/O/G/O Introduction to PL/SQL Oracle Database 10g Develop PLSQL Program Units.
INTRODUCTION TO PL/SQL. Class Agenda Introduction Introduction to PL/SQL Declaring PL/SQL Variable Creating the Executable Section Interacting with the.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL : INTRODUCTION. PL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database language. With PL/SQL, you.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
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.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
PL SQL Block Structures. What is PL SQL A good way to get acquainted with PL/SQL is to look at a sample program. PL/SQL combines the data manipulating.
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Database Application Development using PL/SQL Programming.
Introduction to PL-SQL Introduction to PL-SQL Aj. ประวัฒน์ เปรมธีรสมบูรณ์ Lecture by.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Introduction to PL/SQL. Objectives  After completing this lesson, you should be able to do the following:  Explain the need for PL/SQL  Explain the.
1 PL/SQLPL/SQL Declaring Variables Declaring Variables Declaring Variables Declaring Variables Writing Executable Statements Writing Executable Statements.
Copyright  Oracle Corporation, All rights reserved. 16 Declaring Variables.
Chapter 9: Advanced SQL and PL/SQL Guide to Oracle 10g.
Variables and control statements in PL\SQL Chapter 10.
Chapter 16 Cursors and Exceptions. Chapter Objectives  Determine when an explicit cursor is required  Declare, open, and close an explicit cursor 
9 Copyright © 2004, Oracle. All rights reserved. Manipulating Large Objects.
Retrieving Data in PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –Recognize the SQL statements that can.
Declaring PL/SQL Variables
PL/SQL INTRODUCTION.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Creating Procedures. PL/SQL Program Construct Tools Constructs Anonymous Block Application procedures or functions Application packages Application Triggers.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Copyright  Oracle Corporation, All rights reserved. 20 Working with Composite Datatypes.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Overview.
6 Copyright © 2004, Oracle. All rights reserved. Working with Composite Data Types.
PL/SQL Writing Executable Statements. PL/SQL Block Syntax and Guidelines Statement can be split across lines, but keywords must not be split Lexical units.
Introduction to PL/SQL N. Dimililer. About PL/SQL –PL/SQL is an extension to SQL with design features of programming languages. –Data manipulation and.
Database An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL An introduction to using Oracle PL/SQL Definition: PL/SQL.
Sheffield Hallam University – November 2006 Advanced Databases PL/SQL 1 Blocks, Variables & Control Structures Lynne Dawson November 2006.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
Preface IIntroduction Course Objectives I-2 Oracle Complete Solution I-3 Course Agenda I-4 Tables Used in This Course I-5 The Order Entry Schema I-6 The.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
Introduction to PL/SQL
PL/SQL Declaring Variables Writing Executable Statements
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Declaring Variables Schedule: Timing Topic 45 minutes Lecture
Interacting with the Oracle Server
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQL MULTIPLE CHOICE QUESTION.
Database Management Systems 2
كتابة الجمل التنفيذية في PL/SQL
Working with Composite Datatypes
variables and control statements in PL\SQL
Database Management Systems 2
PL/SQL Declaring Variables.
MATERI PL/SQL Procedures Functions Packages Database Triggers
Presentation transcript:

PL/SQL Declaring Variables

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)

Block Types [DECLARE] BEGIN -- statements [EXCEPTION] END; Anonymous PROCEDURE name IS BEGIN --statements [EXCEPTION] END; Procedure FUNCTION name RETURN datatype IS BEGIN --statements [EXCEPTION] END; Function

Program Construct Tools Construct: Anonymous blocks Application procedures or functions Application Packages Application triggers Object Types Database Server Construct: Anonymous blocks Stored procedures or functions Stored packages Database triggers Object types

Use of Variables Temporary storage of data Manipulation of stored values Reusability Ease of maintenance

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

Types of Variables PL/SQL variables –Scalar –Composite –Reference –LOB (large objects) Non-PL/SQL variables –Bind variables –Host variables

Using iSQL*Plus Variables PL/SQL does not have input or output capability of its own You can reference substitution variables within a PL/SQL block with a preceding ampersand iSQL*Plus host (or “bind”) variables can be used to pass run time values out of the PL/SQL block back to the iSQL*Plus environment

Declaring PL/SQL Variables Syntax: Identifier [CONSTANT] datatype [NOT NULL] [:= | DEFAULT expr]; Example: 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;

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.

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. –The names of the variables must not be longer than 30 characters. The first character must be a letter; the remaining characters can be letters, numbers, or special symbols 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

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

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

Scalar Datatypes Hold a single value Have no internal components 25-OCT 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

Base Scalar DatatypesScalar –VARCHAR2 (maximum_length) –NUMBER [(precision, scale)] –DATE –CHAR [(maximum_length)] –LONG –LONG RAW –BOOLEAN –BINARY_INTEGER –PLS_INTEGER

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

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 Syntax: IdentifierTable.column_name%TYPE;

Declaring Variables with the %TYPE Attribute 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;...

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

Composite Data Types Have internal components that can be manipulated individually Composite data types are of TABLE, RECORD, NESTED TABLE, and VARRAY types

LOB Datatype Variables Recipe(CLOB) Photo(BLOB) Movie(BFILE) NCLOB

Bind Variables Server O/S Bind Variable

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 (:). :g_monthly_sal := v_sal / 12;

Example: VARIABLE g_salary NUMBER BEGIN SELECT salary INTO :g_salary FROM emp WHERE emp_id = 178; END; / PRINT g_salary

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

Example SET SERVEROUTPUT ON DEFINE p_annual_sal = 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; /