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.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Virtual training week 4 structured query language (SQL)
Advanced Package Concepts. 2 home back first prev next last What Will I Learn? Write packages that use the overloading feature Write packages that use.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
1 Copyright © 2004, Oracle. All rights reserved. Creating Stored Procedures.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
Copyright © SUPINFO. All rights reserved Procedures and functions.
Interacting With The Oracle Server. Objectives After completing this lesson, you should be able to do the following: Write a successful SELECT statement.
Program with PL/SQL. Interacting with the Oracle Server.
Lecture 8 Creating Stored Functions. Objectives  After completing this lesson, you should be able to do the following:  What is Function?  Types of.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
Oracle 11g DATABASE DEVELOPMENT LAB1. Introduction  Oracle 11g Database:-  Oracle 11g database is designed for some features, which helps to the organizations.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Trapping Oracle Server Exceptions. 2 home back first prev next last What Will I Learn? Describe and provide an example of an error defined by the Oracle.
Manipulating Data in PL/SQL. 2 home back first prev next last What Will I Learn? Construct and execute PL/SQL statements that manipulate data with DML.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Copyright  Oracle Corporation, All rights reserved. 18 Interacting with the Oracle Server.
Handling Exceptions. 2 home back first prev next last What Will I Learn? Describe several advantages of including exception handling code in PL/SQL Describe.
Creating DDL and Database Event Triggers. 2 home back first prev next last What Will I Learn? Describe events that cause DDL and database event triggers.
PL/SQLPL/SQL Oracle10g Developer: PL/SQL Programming Chapter 9 Database Triggers.
INSERT Statement. 2 home back first prev next last What Will I Learn? Give examples of why it is important to be able to alter the data in a database.
ITEC 224 Database Programming PL/SQL Lab Cursors.
Variables and control statements in PL\SQL Chapter 10.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Oracle10g Developer: PL/SQL Programming1 Objectives SQL queries within PL/SQL Host or bind variables The %TYPE attribute Include queries and control structures.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
implicit and an explicit cursor
Creating Functions. V 12 NE - Oracle 2006 Overview of Stored Functions A function is a named PL/SQL block that returns a value A function can be stored.
Using Functions in SQL Statements. 2 home back first prev next last What Will I Learn? List the advantages of user-defined functions in SQL statements.
Handling Exceptions. Objectives What is exception Types of exceptions How to handle exceptions Trapping pre defined oracle errors.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Stored Procedures and Functions Pemrograman Basis Data MI2183.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
7 Copyright © 2004, Oracle. All rights reserved. Using Explicit Cursors.
ITEC 224 Database Programming
Creating Stored Functions
Interacting with the Oracle8 Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Creating Stored Procedures and Functions
ATS Application Programming: Java Programming
Interacting with the Oracle Server
Manipulating Data.
Database Management Systems 2
Interacting with the Oracle Server
Database Management Systems 2
Database Management Systems 2
SQL 101.
DATABASE MANAGEMENT SYSTEM
Handling Exceptions.
Database Management Systems 2
variables and control statements in PL\SQL
مقدمة في قواعد البيانات
Manipulating Data.
SQL .. An overview lecture3.
SQL-1 Week 8-9.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Oracle9i Developer: PL/SQL Programming Chapter 8 Database Triggers.
Presentation transcript:

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 be directly included in a PL/SQL executable block –Construct and execute an INTO clause to hold the values returned by a single-row SQL SELECT statement –Construct statements to retrieve data that follow good practice guidelines –Construct statements that apply good practice guidelines for naming variables

3 home back first prev next last Why Learn It? In this lesson, you learn to embed standard SQL SELECT statements in PL/SQL blocks. You also learn the importance of following usage guidelines and naming convention guidelines when retrieving data.

4 home back first prev next last SQL Statements in PL/SQL You can use the following kinds of SQL statements in PL/SQL: –SELECT to retrieve data from the database –DML statements such as INSERT, UPDATE, and DELETE to make changes to rows in the database –Transaction control statements such as COMMIT, ROLLBACK, or SAVEPOINT. You use transaction control statements to make the changes to the database permanent or to discard them. Transaction control statements will be covered later in the course. This lesson covers SELECT statements.

5 home back first prev next last SQL Statements in PL/SQL DDL and DCL cannot be used directly in PL/SQL. PL/SQL does not directly support data definition language (DDL) statements, such as CREATE TABLE, ALTER TABLE, or DROP TABLE and DCL statements such as GRANT and REVOKE. –DDL and DCL statements cannot be directly executed because they are constructed and executed at run time. That is, they are dynamic. –Static SQL statements are statements that are fixed at the time a program is compiled. –Static or dynamic means the database structure here.

6 home back first prev next last SELECT Statements in PL/SQL Retrieve data from the database with a SELECT statement. Syntax:

7 home back first prev next last SELECT Statements in PL/SQL The INTO clause is mandatory and occurs between the SELECT and FROM clauses. It is used to specify the names of PL/SQL variables that hold the values that SQL returns from the SELECT clause. You must specify one variable for each item selected, and the order of the variables must correspond with the items selected. DECLARE v_country_name wf_countries.country_name%TYPE; BEGIN SELECT country_name INTO v_country_name FROM wf_countries WHERE country_id = 359; DBMS_OUTPUT.PUT_LINE(' The country name is :' || v_country_name); END;

8 home back first prev next last Retrieving Data in PL/SQL Retrieve hire_date and salary for the specified employee. DECLARE v_emp_hiredate employees.hire_date%TYPE; v_emp_salary employees.salary%TYPE; BEGIN SELECT hire_date, salary INTO v_emp_hiredate, v_emp_salary FROM employees WHERE employee_id = 100; DBMS_OUTPUT.PUT_LINE('Hiredate is: ' || v_emp_hiredate || ' and Salary is: ' || v_emp_salary); END;

9 home back first prev next last Retrieving Data in PL/SQL SELECT statements within a PL/SQL block fall into the ANSI classification of embedded SQL, for which the following rule applies: –queries must return exactly one row. –A query that returns more than one row or no rows generates an error. –You will learn about error handling later in the course. DECLARE v_salary employees.salary%TYPE; BEGIN SELECT salary INTO v_salary FROM employees; DBMS_OUTPUT.PUT_LINE(' Salary is : ' || v_salary); END;

10 home back first prev next last Retrieving Data in PL/SQL Return the sum of the salaries for all the employees in the specified department. DECLARE v_sum_sal NUMBER(10, 2); v_deptno NUMBER NOT NULL := 60; BEGIN SELECT SUM(salary) -- group function INTO v_sum_sal FROM employees WHERE department_id = v_deptno; DBMS_OUTPUT.PUT_LINE('The sum of salary is ' || v_sum_sal); END;

11 home back first prev next last Guidelines for Retrieving Data in PL/SQL Terminate each SQL statement with a semicolon (;). Every value retrieved must be stored in a variable using the INTO clause. The WHERE clause is optional and can contain input variables, constants, literals, or PL/SQL expressions. –However, you should fetch only one row and the usage of the WHERE clause is therefore needed in nearly all cases. Specify the same number of variables in the INTO clause as database columns in the SELECT clause. Be sure that they correspond positionally and that their data types are compatible. Declare the receiving variables using %TYPE.

12 home back first prev next last Guidelines for Naming Conventions In potentially ambiguous SQL statements, the names of database columns take precedence over the names of local variables. DECLARE v_hire_date employees.hire_date%TYPE; employee_id employees.employee_id%TYPE := 176; BEGIN SELECT hire_date INTO v_hire_date FROM employees WHERE employee_id = employee_id; END;

13 home back first prev next last Guidelines for Naming Conventions What is deleted in the following PL/SQL block? DECLARE last_name VARCHAR2(25) := 'King'; BEGIN DELETE FROM emp_dup WHERE last_name = last_name; END;

14 home back first prev next last Guidelines for Naming Conventions Use a naming convention to avoid ambiguity in the WHERE clause. Avoid using database column names as identifiers. Errors can occur during execution because PL/SQL checks the database first for a column in the table. The names of local variables and formal parameters take precedence over the names of database tables (in a PL/SQL statement). The names of database table columns take precedence over the names of local variables.

15 home back first prev next last Guidelines for Naming Conventions The names of local variables and formal parameters take precedence over the names of database tables (in a PL/SQL statement). DECLARE departments employees.hire_date%TYPE; BEGIN SELECT hire_date INTO departments FROM employees WHERE employee_id = 176; DBMS_OUTPUT.PUT_LINE (departments); END; There is a table named departments,but the program can execute successfully. The identifier departments stands for a variable here.

16 home back first prev next last Summary In this lesson, you learned to: –Recognize the SQL statements that can be directly included in a PL/SQL executable block –Construct and execute an INTO clause to hold the values returned by a single-row SQL SELECT statement –Construct statements to retrieve data that follow good practice guidelines –Construct statements that apply good practice guidelines for naming variables

17 home back first prev next last Try It/Solve It The exercises in this lesson cover the following topics: –Recognizing SQL statements that can be directly included in a PL/SQL executable block –Using the INTO clause to hold the values returned by a single-row SQL SELECT statement –Following guidelines for retrieving data –Following guidelines for naming variables