3 Copyright © 2004, Oracle. All rights reserved. Writing Executable Statements.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 2 Single-Row Functions.
Advertisements

Objectives After completing this lesson, you should be able to do the following: Describe various types of conversion functions that are available in.
PL/SQL.
Lecture-5 Though SQL is the natural language of the DBA, it suffers from various inherent disadvantages, when used as a conventional programming language.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 2: Introduction to C++.
JavaScript, Third Edition
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Ch. 3 Single-Row Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
3-1 Copyright  Oracle Corporation, All rights reserved. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Good Programming Practices. 2 home back first prev next last What Will I Learn? List examples of good programming practices Accurately insert comments.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
L/O/G/O Introduction to PL/SQL Oracle Database 10g Develop PLSQL Program Units.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Program with PL/SQL Lesson 5. Working with Composite Data Types.
Conversion Functions Implicit datatype conversion Explicit datatype conversion Datatypeconversion In some cases, Oracle Server allows data of one datatype.
Single – Row Functions. Objectives After completing this lesson, you should be able to do the following:  Describe various types of functions available.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
PL/SQL Block Structure DECLARE - Optional Variables, cursors, user-defined exceptions BEGIN - Mandatory SQL Statements PL/SQL Statements EXCEPTIONS - Optional.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Introduction to SQL PART Ⅰ 第一讲 Writing Basic SQL SELECT Statements.
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 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Conversion Functions.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Declaring PL/SQL Variables
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Introduction to PL/SQL Francis Thottungal. The outline The basic PL/SQL code structure is : DECLARE -- optional, which declares and define variables,
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
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.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Sheffield Hallam University – November 2006 Advanced Databases PL/SQL 1 Blocks, Variables & Control Structures Lynne Dawson November 2006.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
4 Copyright © 2009, Oracle. All rights reserved. Using Conversion Functions and Conditional Expressions.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
PL/SQL Part B.
Retrieving Data Using the SQL SELECT Statement
Oracle11g: PL/SQL Programming Chapter 2 Basic PL/SQL Block Structures.
Section 3.2c Strings and Method Signatures
PL / SQL Basics Chapter 3.
Java Programming: From Problem Analysis to Program Design, 4e
Database Management Systems 2
2.1 Parts of a C++ Program.
Chapter 3 The DATA DIVISION.
Introduction to PL/SQL
Chapter 2: Basic Elements of Java
Chapter 4: Introduction to PL/SQL
Writing Control Structures
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

3 Copyright © 2004, Oracle. All rights reserved. Writing Executable Statements

3-2 Copyright © 2004, Oracle. All rights reserved. Objectives After completing this lesson, you should be able to do the following: Identify lexical units in a PL/SQL block Use built-in SQL functions in PL/SQL Describe when implicit conversions take place and when explicit conversions have to be dealt with Write nested blocks and qualify variables with labels Write readable code with appropriate indentations

3-3 Copyright © 2004, Oracle. All rights reserved. Lexical Units in a PL/SQL Block Lexical units: Are building blocks of any PL/SQL block Are sequences of characters including letters, digits, tabs, spaces, returns, and symbols Can be classified as: –Identifiers –Delimiters –Literals –Comments

3-4 Copyright © 2004, Oracle. All rights reserved.

3-5 Copyright © 2004, Oracle. All rights reserved. PL/SQL Block Syntax and Guidelines Literals: –Character and date literals must be enclosed in single quotation marks. –Numbers can be simple values or scientific notation. Statements can continue over several lines. name := 'Henderson';

3-6 Copyright © 2004, Oracle. All rights reserved. Commenting Code Prefix single-line comments with two dashes (--). Place multiple-line comments between the symbols “/*” and “*/”. Example: DECLARE... annual_sal NUMBER (9,2); BEGIN -- Begin the executable section /* Compute the annual salary based on the monthly salary input from the user */ annual_sal := monthly_sal * 12; END; -- This is the end of the block /

3-7 Copyright © 2004, Oracle. All rights reserved. SQL Functions in PL/SQL Available in procedural statements: –Single-row number –Single-row character –Data type conversion –Date –Timestamp – GREATEST and LEAST –Miscellaneous functions Not available in procedural statements: – DECODE –Group functions

3-8 Copyright © 2004, Oracle. All rights reserved. SQL Functions in PL/SQL: Examples Get the length of a string: Convert the employee name to lowercase: desc_size INTEGER(5); prod_description VARCHAR2(70):='You can use this product with your radios for higher frequency'; -- get the length of the string in prod_description desc_size:= LENGTH(prod_description); emp_name:= LOWER(emp_name);

3-9 Copyright © 2004, Oracle. All rights reserved. Data Type Conversion Convert data to comparable data types Are of two types: –Implicit conversions –Explicit conversions Some conversion functions: – TO_CHAR – TO_DATE – TO_NUMBER – TO_TIMESTAMP

3-10 Copyright © 2004, Oracle. All rights reserved. Data Type Conversions

3-11 Copyright © 2004, Oracle. All rights reserved. Data Type Conversion date_of_joining DATE:= '02-Feb-2000'; date_of_joining DATE:= 'February 02,2000'; date_of_joining DATE:= TO_DATE('February 02,2000','Month DD, YYYY'); 1 2 3

3-12 Copyright © 2004, Oracle. All rights reserved. Nested Blocks PL/SQL blocks can be nested. An executable section ( BEGIN … END ) can contain nested blocks. An exception section can contain nested blocks.

3-13 Copyright © 2004, Oracle. All rights reserved. Nested Blocks DECLARE outer_variable VARCHAR2(20):='GLOBAL VARIABLE'; BEGIN DECLARE inner_variable VARCHAR2(20):='LOCAL VARIABLE'; BEGIN DBMS_OUTPUT.PUT_LINE(inner_variable); DBMS_OUTPUT.PUT_LINE(outer_variable); END; DBMS_OUTPUT.PUT_LINE(outer_variable); END; / Example:

3-14 Copyright © 2004, Oracle. All rights reserved. Variable Scope and Visibility DECLARE father_name VARCHAR2(20):='Patrick'; date_of_birth DATE:='20-Apr-1972'; BEGIN DECLARE child_name VARCHAR2(20):='Mike'; date_of_birth DATE:='12-Dec-2002'; BEGIN DBMS_OUTPUT.PUT_LINE('Father''s Name: '||father_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth); DBMS_OUTPUT.PUT_LINE('Child''s Name: '||child_name); END; DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth); END; / 1 2

3-15 Copyright © 2004, Oracle. All rights reserved. Variable Scope and Visibility

3-16 Copyright © 2004, Oracle. All rights reserved. Qualify an Identifier > DECLARE father_name VARCHAR2(20):='Patrick'; date_of_birth DATE:='20-Apr-1972'; BEGIN DECLARE child_name VARCHAR2(20):='Mike'; date_of_birth DATE:='12-Dec-2002'; BEGIN DBMS_OUTPUT.PUT_LINE('Father''s Name: '||father_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: ' ||outer.date_of_birth); DBMS_OUTPUT.PUT_LINE('Child''s Name: '||child_name); DBMS_OUTPUT.PUT_LINE('Date of Birth: '||date_of_birth); END; /`

3-17 Copyright © 2004, Oracle. All rights reserved. Determining Variable Scope > DECLARE sal NUMBER(7,2) := 60000; comm NUMBER(7,2) := sal * 0.20; message VARCHAR2(255) := ' eligible for commission'; BEGIN DECLARE sal NUMBER(7,2) := 50000; comm NUMBER(7,2) := 0; total_comp NUMBER(7,2) := sal + comm; BEGIN message := 'CLERK not'||message; outer.comm := sal * 0.30; END; message := 'SALESMAN'||message; END; / 1 2

3-18 Copyright © 2004, Oracle. All rights reserved. Operators in PL/SQL Logical Arithmetic Concatenation Parentheses to control order of operations Exponential operator (**) Same as in SQL }

3-19 Copyright © 2004, Oracle. All rights reserved. Operators in PL/SQL Examples: Increment the counter for a loop. Set the value of a Boolean flag. Validate whether an employee number contains a value. loop_count := loop_count + 1; good_sal := sal BETWEEN AND ; valid:= (empno IS NOT NULL);

3-20 Copyright © 2004, Oracle. All rights reserved. Programming Guidelines Make code maintenance easier by: Documenting code with comments Developing a case convention for the code Developing naming conventions for identifiers and other objects Enhancing readability by indenting

3-21 Copyright © 2004, Oracle. All rights reserved. Indenting Code For clarity, indent each level of code. Example: BEGIN IF x=0 THEN y:=1; END IF; END; / DECLARE deptno NUMBER(4); location_id NUMBER(4); BEGIN SELECTdepartment_id, location_id INTOdeptno, location_id FROMdepartments WHEREdepartment_name = 'Sales';... END; /

3-22 Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to: Use built-in SQL functions in PL/SQL Write nested blocks to break logically related functionalities Decide when you should perform explicit conversions Qualify variables in nested blocks

3-23 Copyright © 2004, Oracle. All rights reserved. Practice 3 Overview This practice covers the following topics: Reviewing scoping and nesting rules Writing and testing PL/SQL blocks

3-24 Copyright © 2004, Oracle. All rights reserved.

3-25 Copyright © 2004, Oracle. All rights reserved.

3-26 Copyright © 2004, Oracle. All rights reserved.

3-27 Copyright © 2004, Oracle. All rights reserved.

3-28 Copyright © 2004, Oracle. All rights reserved.