PL/SQL Declaring Variables.

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

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
SQL*PLUS, PLSQL and SQLLDR Ali Obaidi. SQL Advantages High level – Builds on relational algebra and calculus – Powerful operations – Enables automatic.
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.
Bordoloi and Bock PL/SQL : INTRODUCTION. Bordoloi and BockPL/SQL PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational.
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.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
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.
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.
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.
11 Copyright س Oracle Corporation, All rights reserved. ® Overview of PL/SQL.
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.
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 Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
PL/SQL Procedural Language / Structured Query Language.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
CIS4368: Advanced DatabaseSlide # 1 PL/SQL Dr. Peeter KirsSpring, 2003 PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced 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.
PL/SQL Declaring Variables PL/SQL Block Structure DECLARE (Optional) Variables, cursors, user-defined exceptions BEGIN (Mandatory) - SQL statements -
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.
School of Computing and Management Sciences © Sheffield Hallam University SQL is non-procedural –designed to be relatively approachable to non- programmers.
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.
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 
Declaring PL/SQL Variables
PL/SQL INTRODUCTION.
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,
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
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.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
C LANGUAGE MULITPLE CHOICE QUESTION SET-2
Introduction to PL/SQL
PL/SQL Declaring Variables Writing Executable Statements
Creating Stored Functions
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Program with PL/SQL Lesson 4.
Declaring Variables Schedule: Timing Topic 45 minutes Lecture
Interacting with the Oracle Server
PL/SQL.
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
PL/SQL MULTIPLE CHOICE QUESTION.
Introduction to PL/SQL
PL/SQL Scripting in Oracle:
Introduction to PL/SQL
Sa0951a PL/SQL 1: Introduction
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
variables and control statements in PL\SQL
Database Management Systems 2
PL/SQL week10.
Chapter 8 Advanced SQL.
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 [DECLARE] BEGIN --statements [EXCEPTION] END;

Program Construct Tools Construct: Anonymous blocks 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 Non-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];

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

Scalar Data Types Hold a single value Have no internal components Classified into 4 categories: NUMBER CHARACTER DATE BOOLEAN

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: Identifier Table.column_name%TYPE;

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 Data Type Variables CLOB  Book BLOB  Photo BFILE  Movie NCLOB  NCHAR

Bind Variables A variable that declare in a host environment Can be used to pass run-time values To reference in PL/SQL, you must prefix its name with a colon (:) 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 = 60000 DECLARE v_sal NUMBER(9,2) := &p_annual_sal; BEGIN v_sal := v_sal/12; DBMS_OUTPUT.PUT_LINE (‘The monthly salary is ‘||TO_CHAR(v_sal)); END; /