Using Variables in PL/SQL. Copyright 2007, Oracle. All rights reserved.



Similar documents
Creating PL/SQL Blocks. Copyright 2007, Oracle. All rights reserved.

Recognizing PL/SQL Lexical Units. Copyright 2007, Oracle. All rights reserved.

Oracle Internal & Oracle Academy

What is the value of SQL%ISOPEN immediately after the SELECT statement is executed? Error. That attribute does not apply for implicit cursors.

Database Programming with PL/SQL: Learning Objectives

2. Which of the following declarations is invalid? Mark for Review (1) Points

PL / SQL Basics. Chapter 3

Oracle Database: SQL and PL/SQL Fundamentals

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Answers to the Try It Yourself Sections

Oracle Database: SQL and PL/SQL Fundamentals NEW

Handling Exceptions. Copyright 2008, Oracle. All rights reserved.

Oracle Database: SQL and PL/SQL Fundamentals

PL/SQL MOCK TEST PL/SQL MOCK TEST I

PL/SQL Overview. Basic Structure and Syntax of PL/SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Oracle Database 10g: Program with PL/SQL

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database: Develop PL/SQL Program Units

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.

Oracle Database 10g: Introduction to SQL

Oracle 10g PL/SQL Training

Oracle(PL/SQL) Training

A basic create statement for a simple student table would look like the following.

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database: SQL and PL/SQL Fundamentals NEW

Review your answers, feedback, and question scores below. An asterisk (*) indicates a correct answer.


Oracle Database: Program with PL/SQL

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

PL/SQL Programming Concepts: Review. Copyright 2004, Oracle. All rights reserved.

ISYS PL/SQL Basics. Week 03

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

Oracle 11g PL/SQL training

ICT. PHP coding. Universityy. in any

Oracle Database: Program with PL/SQL

Oracle10g PL/SQL Programming V1.0

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Oracle Database: Program with PL/SQL

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

Oracle Database: Program with PL/SQL

PL/SQL TUTORIAL. Simply Easy Learning by tutorialspoint.com. tutorialspoint.com

Writing Control Structures

Oracle For Beginners Page : 1

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Best Practices for Dynamic SQL

Oracle Database: Program with PL/SQL

SQL Server Database Coding Standards and Guidelines

Handling Exceptions. Schedule: Timing Topic 45 minutes Lecture 20 minutes Practice 65 minutes Total

STUDY OF PL/SQL BLOCK AIM: To Study about PL/SQL block.

Oracle PL/SQL Language. CIS 331: Introduction to Database Systems

A Tutorial on Stored procedures and user defined functions

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Introduction to Oracle PL/SQL Programming V2.1 - Lessons 11-End

Oracle Single Sign-On

Microsoft Access 3: Understanding and Creating Queries

Programming with SQL

Handling PL/SQL Errors

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Teach Yourself InterBase

Oracle Database 11g: Program with PL/SQL

3.GETTING STARTED WITH ORACLE8i

Oracle For Beginners Page : 1

Instant SQL Programming

Spool Generated For Class of Oracle By Satish K Yellanki

GENERAL PROGRAMMING LANGUAGE FUNDAMENTALS

Developing SQL and PL/SQL with JDeveloper

APEX_ITEM and Dynamic Tabular Forms

Using Temporary Tables to Improve Performance for SQL Data Services

Oracle Database 11g: Advanced PL/SQL

Progress Embedded SQL-92 Guide and Reference

An Introduction to PL/SQL. Mehdi Azarmi

How To Create A Table In Sql (Ahem)

Firebird. Embedded SQL Guide for RM/Cobol

Introduction This document s purpose is to define Microsoft SQL server database design standards.

MOC 20461C: Querying Microsoft SQL Server. Course Overview

KB_SQL SQL Reference Guide Version 4

Information Systems SQL. Nikolaj Popov

Using SQL Queries in Crystal Reports

SQL NULL s, Constraints, Triggers

Training Guide. PL/SQL for Beginners. Workbook

Dell Boomi : Efficient Database Integration using Dell Boomi

Darshan Institute of Engineering & Technology PL_SQL

Oracle Migration Workbench

Data Tool Platform SQL Development Tools

Chapter 1. Writing Basic. SQL Statements

PROC SQL for SQL Die-hards Jessica Bennett, Advance America, Spartanburg, SC Barbara Ross, Flexshopper LLC, Boca Raton, FL

MS Access: Advanced Tables and Queries. Lesson Notes Author: Pamela Schmidt

Passing 1D arrays to functions.

Overhauling PL/SQL Applications for Optimized Performance

Data Integrator. Pervasive Software, Inc B Riata Trace Parkway Austin, Texas USA

Continuous Integration Part 2

SQL. Short introduction

Maintaining Stored Procedures in Database Application

Structured Query Language. Telemark University College Department of Electrical Engineering, Information Technology and Cybernetics

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Overview of PL/SQL. About PL/SQL

Transcription:

What Will I Learn? In this lesson, you will learn to: List the uses of variables in PL/SQL Identify the syntax for variables in PL/SQL Declare and initialize variables in PL/SQL Assign new values to variables in PL/SQL 2

Why Learn It? Variables are used for storing and manipulating data. In this lesson, you learn how to declare and initialize variables in the declarative section of a PL/SQL block. With PL/SQL, you can declare variables and then use them in SQL and procedural statements. 3

Use of Variables Variables can be used for: Temporary storage of data Manipulation of stored values Reusability SELECT first_name, department_id INTO v_emp_fname, v_emp_deptno FROM Jennifer v_emp_fname 10 v_emp_deptno 4

Handling Variables in PL/SQL Variables are: Declared and initialized in the declarative section Used and assigned new values in the executable section Variables can be: Passed as parameters to PL/SQL subprograms Assigned to hold the output of a PL/SQL subprogram 5

Declaring and Initializing PL/SQL Variables All PL/SQL variables must be declared in the declaration section before referencing them in the PL/SQL block. The purpose of a declaration is to allocate storage space for a value, specify its data type, and name the storage location so that you can reference it. Variables can be declared in the declarative part of any PL/SQL block, subprogram, or package. 6

Declaring and Initializing Variables: Syntax Syntax: identifier [CONSTANT] datatype [NOT NULL] [:= expr DEFAULT expr]; identifier is the name of the variable CONSTANT constrains the variable so that its value cannot change; constants must be initialized datatype is a scalar, composite, reference, or LOB data type (This course covers only scalar, composite, and LOB data types.) 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 expression, another variable, or an expression involving operators and functions 7

Declaring and Initializing Variables: Syntax Syntax: identifier [CONSTANT] datatype [NOT NULL] [:= expr DEFAULT expr]; Conventions: The lowercase italic represents variables or placeholders. Brackets ([ ]) enclose one or more optional items. Do not insert the brackets. A vertical bar ( ) represents a choice of two or more options within brackets. Do not insert the vertical bar. 8

Declaring and Initializing Variables: Examples DECLARE v_emp_hiredate DATE; v_emp_deptno NUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := 'Atlanta'; c_comm CONSTANT NUMBER := 1400; v_population INTEGER; v_book_type VARCHAR2(20) DEFAULT 'fiction'; v_artist_name VARCHAR2(50); v_firstname VARCHAR2(20):='Rajiv'; v_lastname VARCHAR2(20) DEFAULT 'Kumar'; c_display_no CONSTANT PLS_INTEGER := 20; 9

Assigning Values in the Executable Section After a variable is declared, you can use it in the executable section of a PL/SQL block. For example, in the following block, the variable v_myname is declared in the declarative section of the block. This variable can be accessed in the executable section of the same block. What do you think the block will print? DECLARE v_myname VARCHAR2(20); BEGIN DBMS_OUTPUT.PUT_LINE('My name is: ' v_myname); v_myname := 'John'; DBMS_OUTPUT.PUT_LINE('My name is: ' v_myname); END; 10

Assigning Values in the Executable Section In this example, the value John is assigned to the variable in the executable section. The value of the variable is concatenated with the string My name is:. The output is: 11

Assigning Values in the Executable Section In this block, the variable v_myname is declared and initialized in the declarative section. v_myname will hold the value John after initialization. Note that this value is manipulated in the executable section of the block. DECLARE v_myname VARCHAR2(20):= 'John'; BEGIN v_myname := 'Steven'; DBMS_OUTPUT.PUT_LINE('My name is: ' v_myname); END; Output: 12

Passing Variables as Parameters to PL/SQL Subprograms Parameters are values passed to a program by the user or by another program to customize the program. In PL/SQL, subprograms can take parameters. You can pass variables as parameters to procedures and functions. In the following example, the parameter v_date is being passed to the procedure PUT_LINE, which is part of the package, DBMS_OUTPUT. DECLARE v_date VARCHAR2(30); BEGIN SELECT TO_CHAR(SYSDATE) INTO v_date FROM DUAL; DBMS_OUTPUT.PUT_LINE(v_date); END; 13

Assigning Variables to PL/SQL Subprogram Output Variables can be used to hold the value that is returned by a function. --function to return number of characters in string FUNCTION num_characters (p_string IN VARCHAR2) RETURN INTEGER IS v_num_characters INTEGER; BEGIN SELECT LENGTH(p_string) INTO v_num_characters FROM DUAL; RETURN v_num_characters; END; --anonymous block: assign variable to function output DECLARE v_length_of_string INTEGER; BEGIN v_length_of_string := LENGTH('Oracle Corporation'); DBMS_OUTPUT.PUT_LINE(v_length_of_string); END; 14

Try It/Solve It Terminology Key terms used in this lesson include: Variables Parameters 15

Summary In this lesson, you have learned how to: List the uses of variables in PL/SQL Identify the syntax for variables in PL/SQL Declare and initialize variables in PL/SQL Assign new values to variables in PL/SQL 16

Try It/Solve It The exercises in this lesson cover the following topics: Listing the uses of variables in PL/SQL Determining valid variable declarations in PL/SQL Declaring and initializing variables in PL/SQL Assigning new values to variables in PL/SQL 17