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.

Slides:



Advertisements
Similar presentations
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
Advertisements

ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
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.
Creating Triggers.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
Oracle10g Developer: PL/SQL Programming1 Objectives Manipulating data with cursors Managing errors with exception handlers Addressing exception-handling.
Chapter 4 Cursors and Exception Handling Oracle10g Developer:
Benefits of PL/SQL. 2 home back first prev next last What Will I Learn? In this lesson, you will learn to: –List and explain the benefits of PL/SQL –List.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
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.
DEFAULT Values and the MERGE Statement. 2 home back first prev next last What Will I Learn? Understand when to specify a DEFAULT value. Construct and.
9 Copyright © 2007, Oracle. All rights reserved. Managing Data and Concurrency.
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.
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.
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.
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.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
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.
Controlling User Access. 2 home back first prev next last What Will I Learn? Compare the difference between object privileges and system privileges Construct.
Using SQL in PL/SQL ITEC 224 Database Programming.
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.
Introduction to Explicit Cursors. 2 home back first prev next last What Will I Learn? Distinguish between an implicit and an explicit cursor Describe.
Cursor FOR Loops. 2 home back first prev next last What Will I Learn? List and explain the benefits of using cursor FOR loops Create PL/SQL code to declare.
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.
Oracle11g: PL/SQL Programming Chapter 3 Handling Data in PL/SQL Blocks.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
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.
1 PL/SQL Part C Scope and Interacting with the Oracle Server.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
I Copyright © 2007, Oracle. All rights reserved. Introduction.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Copyright © 2004, Oracle. All rights reserved. M ANIPULATING D ATA.
STORED PROCEDURE & STORED FUNCTION Politeknik Telkom 2012.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
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
CS322: Database Systems PL/ SQL PL/SQL by Ivan Bayross.
Interacting with the Oracle8 Server
Creating Database Triggers
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Interacting with the Oracle Server
Manipulating Data.
Database Management Systems 2
Interacting with the Oracle Server
Database Management Systems 2
Database Management Systems 2
Using the Set Operators
Manipulating Data.
Cursors.
Manipulating Data.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Chapter 8 Advanced SQL.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Manipulating Data Lesson 3.
Presentation transcript:

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 statements Describe when to use implicit or explicit cursors in PL/SQL Create PL/SQL code to use SQL implicit cursor attributes to evaluate cursor activity

3 home back first prev next last Why Learn It? In the previous lesson, you learned that you can include SELECT statements that return a single row in a PL/SQL block. The data retrieved by the SELECT statement must be held in variables using the INTO clause. In this lesson, you learn how to include data manipulation language (DML) statements such as INSERT, UPDATE, DELETE, and MERGE in PL/SQL blocks.

4 home back first prev next last Manipulating Data Using PL/SQL Make changes to data by using DML commands within your PLSQL block: –INSERT –UPDATE –DELETE –MERGE

5 home back first prev next last Manipulating Data Using PL/SQL You manipulate data in the database by using the DML commands. You can issue the DML commands—INSERT, UPDATE, DELETE and MERGE—without restriction in PL/SQL. –Row locks (and table locks) are released by including COMMIT or ROLLBACK statements in the PL/SQL code. The MERGE statement selects rows from one table to update and/or insert into another table. –The decision whether to update or insert into the target table is based on a condition in the ON clause. –Note: MERGE is a deterministic statement—that is, you cannot update the same row of the target table multiple times in the same MERGE statement. –You must have INSERT and UPDATE object privileges in the target table and the SELECT privilege in the source table.

6 home back first prev next last Inserting Data The INSERT statement adds new row(s) to a table. –Example: Add new employee information to the COPY_EMP table. BEGIN INSERT INTO copy_emp (employee_id, first_name, last_name, , hire_date, job_id, salary) VALUES (99, 'Ruth', 'Cores', 'RCORES', SYSDATE, 'AD_ASST', 4000); END; One new row will be added to the COPY_EMP table.

7 home back first prev next last Updating Data The UPDATE statement modifies existing row(s) in a table. –Example: Increase the salary of all employees who are stock clerks. DECLARE v_sal_increase employees.salary%TYPE := 800; BEGIN UPDATE copy_emp SET salary = salary + v_sal_increase WHERE job_id = 'ST_CLERK'; END;

8 home back first prev next last Deleting Data The DELETE statement removes row(s) from a table. –Example: Delete rows that belong to department 10 from the COPY_EMP table. DECLARE v_deptno employees.department_id%TYPE := 10; BEGIN DELETE FROM copy_emp WHERE department_id = v_deptno; END;

9 home back first prev next last Merging Rows The MERGE statement selects rows from one table to update and/or insert into another table. Insert or update rows in the copy_emp table to match the employees table.

10 home back first prev next last Getting information from a Cursor Look again at the DELETE statement in this PL/SQL block. DECLARE v_deptno employees.department_id%TYPE := 10; BEGIN DELETE FROM copy_emp WHERE department_id = v_deptno; END; It would be useful to know how many COPY_EMP rows were deleted by this statement. To obtain this information, we need to understand cursors.

11 home back first prev next last What is a Cursor? Every time a SQL statement is about to be executed, the Oracle server allocates a private memory area to store the SQL statement and the data which it uses. –This memory area is called an implicit cursor. –Because this memory area is automatically managed by the Oracle server, you have no direct control over it. –However, you can use predefined PL/SQL variables, called implicit cursor attributes, to find out how many rows were processed by the SQL statement.

12 home back first prev next last Implicit and Explicit Cursors There are two types of cursors: –Implicit cursors:  Defined automatically by Oracle for all SQL data manipulation statements, and for queries that return only one row.  An implicit cursor is always automatically named “SQL” –Explicit cursors:  Defined by the PL/SQL programmer for queries that return more than one row. (Covered in a later lesson.)

13 home back first prev next last Cursor Attributes for Implicit Cursors Cursor attributes are automatically declared variables which allow you to evaluate what happened when a cursor was last used. Attributes for implicit cursors are prefaced with “SQL”. Use these attributes in PL/SQL statements, but not in SQL statements. Using cursor attributes, you can test the outcome of your SQL statements.

14 home back first prev next last Using Implicit Cursor Attributes Example 1 –Delete rows that have the specified employee ID from the copy_emp table. Print the number of rows deleted. DECLARE v_deptno copy_emp.department_id%TYPE := 50; BEGIN DELETE FROM copy_emp WHERE department_id = v_deptno; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows deleted.'); END;

15 home back first prev next last Using Implicit Cursor Attributes Example 2 –Update several rows in the COPY_EMP table. Print the number of rows updated. DECLARE v_sal_increase employees.salary%TYPE := 800; BEGIN UPDATE copy_emp SET salary = salary + v_sal_increase WHERE job_id = 'ST_CLERK'; DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows updated.'); END;

16 home back first prev next last Using Implicit Cursor Attributes Good Practice Guideline –Look at this code, which creates a table and then executes a PL/SQL block. –What value will be inserted into RESULTS? CREATE TABLE results (num_rows NUMBER(4)); create table copy_emp as select * from employees; BEGIN UPDATE copy_emp SET salary = salary WHERE job_id = 'ST_CLERK'; INSERT INTO results (num_rows) VALUES (SQL%ROWCOUNT); END; DECLARE v_count NUMBER; BEGIN UPDATE copy_emp SET salary = salary WHERE job_id = 'ST_CLERK'; v_count := SQL%ROWCOUNT; INSERT INTO results (num_rows) VALUES (v_count); COMMIT; END;

17 home back first prev next last Terminology Key terms used in this lesson include: –INSERT –UPDATE –DELETE –MERGE –Implicit cursors –Explicit cursors

18 home back first prev next last Summary In this lesson, you learned to: –Construct and execute PL/SQL statements that manipulate data with DML statements –Describe when to use implicit or explicit cursors in PL/SQL –Create PL/SQL code to use SQL implicit cursor attributes to evaluate cursor activity

19 home back first prev next last Try It/Solve It The exercises in this lesson cover the following topics: –Executing PL/SQL statements that manipulate data with DML statements –Describing when to use implicit or explicit cursors in PL/SQL –Using SQL implicit cursor attributes in PL/SQL