Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4.

Slides:



Advertisements
Similar presentations
PL/SQL.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
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.
Manipulating Data Schedule: Timing Topic 60 minutes Lecture
ORACLE TRANSACTIONS A transaction begins with the first executable SQL statement after a commit, rollback or connection made to the Oracle engine. All.
Transaction Processing. Objectives After completing this lesson, you should be able to do the following: –Define transactions effectively for an application.
9-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language A DML statement is executed when you: – Add new rows to a table.
Murali Mani Persistent Stored Modules (Stored Procedures) : PSM.
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.
Copyright  Oracle Corporation, All rights reserved. 9 Manipulating Data: INSERT, UPDATE, DELETE.
Chapter 5 Data Manipulation and Transaction Control Oracle 10g: SQL
Cursors in Pl/SQL Database 1. Practice. Sample Database The schema of the sample database is the following: Drinkers (name, occupation, birthday, salary)
ORACLE ONLINE TRAINING Contact our Support Team : SOFTNSOL India: Skype id : softnsoltrainings id:
SQL Within PL / SQL Chapter 4. 2 SQL Within PL / SQL SQL Statements DML in PL / SQL Pseudocolums Transaction Control.
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.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
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:
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.
Stored Procedures, Transactions, and Error-Handling
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 DB2 9 Fundamentals.
Copyright © 2011 Accenture All Rights Reserved. Accenture, its logo, and High Performance Delivered are trademarks of Accenture. SQL Workshop Day 3.
Records Oracle Database PL/SQL 10g Programming Chapter 5.
Program with PL/SQL. Interacting with the Oracle Server.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Using Procedures & Functions Oracle Database PL/SQL 10g Programming Chapter 9.
Quick review of SQL And conversion to Oracle SQL.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
PRACTICE OVERVIEW PL/SQL Part Examine this package specification and body: Which statement about the V_TOTAL_BUDGET variable is true? A. It must.
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.
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.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Database Technology Jing Shen.
Dynamic SQL Oracle Database PL/SQL 10g Programming Chapter 13.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Using SQL in PL/SQL ITEC 224 Database Programming.
Manipulating Data. Objectives After completing this lesson, you should be able to do the following: Describe each DML statement Insert rows into a table.
PL/SQLPL/SQL Oracle11g: PL/SQL Programming Chapter 4 Cursors and Exception Handling.
9 Manipulating Data. 9-2 Objectives At the end of this lesson, you should be able to: Describe each DML statement Insert rows into a table Update rows.
Database Programming Sections 14– database transactions and controlling User Access.
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.
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.
implicit and an explicit cursor
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L12_JDBC_MySQL 1 Transations.
Transactions, Roles & Privileges Oracle and ANSI Standard SQL Lecture 11.
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.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Oracle 10g Database Administrator: Implementation and Administration Chapter 10 Basic Data Management.
Program with PL/SQL Lesson 3. Interacting with the Oracle Server.
Oracle9i Developer: PL/SQL Programming Chapter 5 Functions.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
Oracle 11g: SQL Chapter 5 Data Manipulation and Transaction Control.
1 Copyright © 2004, Oracle. All rights reserved. PL/SQL Programming Concepts: Review.
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.
ITEC 224 Database Programming
Interacting with the Oracle Server
ATS Application Programming: Java Programming
Interacting with the Oracle Server
Introduction to Oracle9i: SQL
Database Management Systems 2
DATABASE MANAGEMENT SYSTEM
Chapter 2 Handling Data in PL/SQL Blocks Oracle9i Developer:
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.
Presentation transcript:

Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 2 Using SQL in PL/SQL Using SQL Statements Using SQL Statements Using SQL Built-in Functions Using SQL Built-in Functions Using Pseudo Columns Using Pseudo Columns Using Cursors Using Cursors Dynamic SQL Statements Dynamic SQL Statements Regular Expressions Regular Expressions

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 3 Using SQL in PL/SQL SQL Command Types Data Control Language (DCL) command can be used directly inside PL/SQL. Data Control Language (DCL) command can be used directly inside PL/SQL. Data Manipulation Language (DML) commands can be used directly inside PL/SQL blocks. Data Manipulation Language (DML) commands can be used directly inside PL/SQL blocks. Data Definition Language (DDL) commands cannot be used directly inside PL/SQL blocks, but they can be used indirectly through dynamic SQL statements. Data Definition Language (DDL) commands cannot be used directly inside PL/SQL blocks, but they can be used indirectly through dynamic SQL statements.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 4 Using SQL in PL/SQL SQL Command Types: DCL Single DML statements are an all or nothing proposition, known as autonomous transactions: Single DML statements are an all or nothing proposition, known as autonomous transactions: You type COMMIT to accept a DML SQL statement. You type COMMIT to accept a DML SQL statement. You type ROLLBACK to reject a DML SQL statement. You type ROLLBACK to reject a DML SQL statement. Two or more DML statements as a set of activities can act as autonomously but can be controlled as groups using DCL commands; and these are known as transactions, not autonomous transactions: Two or more DML statements as a set of activities can act as autonomously but can be controlled as groups using DCL commands; and these are known as transactions, not autonomous transactions: A transaction requires that all DML statements succeed or fail. A transaction requires that all DML statements succeed or fail. A transaction is ACID compliant and has four properties: Atomic, Consistent, Isolated, and Durable. A transaction is ACID compliant and has four properties: Atomic, Consistent, Isolated, and Durable.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 5 Using SQL in PL/SQL SQL Command Types: DCL SAVEPOINT sets a named transaction marker. SAVEPOINT sets a named transaction marker. COMMIT makes permanent any changes made by a user during a session. COMMIT makes permanent any changes made by a user during a session. ROLLBACK undoes any changes made by a user: ROLLBACK undoes any changes made by a user: To the beginning of session when the command does not refer to a SAVEPOINT ; which models autonomous transactions. To the beginning of session when the command does not refer to a SAVEPOINT ; which models autonomous transactions. To the named SAVEPOINT provided as an actual parameter to the ROLLBACK command; which models transactions. To the named SAVEPOINT provided as an actual parameter to the ROLLBACK command; which models transactions.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 6 Using SQL in PL/SQL SQL Command Types: Autonomous Transactions BEGIN UPDATE a_table UPDATE a_table SET name = 'Autonomous' SET name = 'Autonomous' WHERE id = 1; WHERE id = 1;EXCEPTION WHEN others THEN WHEN others THEN ROLLBACK; ROLLBACK;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 7 Using SQL in PL/SQL SQL Command Types: Transactions BEGIN SAVEPOINT beginning; SAVEPOINT beginning; INSERT INTO parent_table INSERT INTO parent_table VALUES (parent_id, name); VALUES (parent_id, name); INSERT INTO child_table INSERT INTO child_table VALUES (child_id, parent_id, name); VALUES (child_id, parent_id, name); COMMIT; COMMIT;EXCEPTION WHEN others THEN WHEN others THEN ROLLBACK TO beginning; ROLLBACK TO beginning;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 8 Using SQL in PL/SQL SQL Command Types: Transactions SET TRANSACTION READ ONLY SET TRANSACTION READ ONLY Constrains the transaction scope of action, which is useful when working in snapshot databases. Constrains the transaction scope of action, which is useful when working in snapshot databases. SET TRANSACTION READ WRITE SET TRANSACTION READ WRITE The default state frees the transaction to write data. The default state frees the transaction to write data. SET TRANSACTION ISOLATION LEVEL READ COMMITTED SET TRANSACTION ISOLATION LEVEL READ COMMITTED Constrains the transaction scope of action, requiring all pending transactions to wait on any locked row. Constrains the transaction scope of action, requiring all pending transactions to wait on any locked row. SET TRANSACTION ISOLATION LEVEL SERIALIZABLE SET TRANSACTION ISOLATION LEVEL SERIALIZABLE Constrains the transaction scope of action, requiring all pending transactions to abort when encountering locked rows. Constrains the transaction scope of action, requiring all pending transactions to abort when encountering locked rows. SET TRANSACTION USE ROLLBACK SEGMENT SET TRANSACTION USE ROLLBACK SEGMENT Constrains the transaction to a named ROLLBACK segment, which enables you to target large transactions to large ROLLBACK segments, but this is not generally used when you’re using automatic undo management. Constrains the transaction to a named ROLLBACK segment, which enables you to target large transactions to large ROLLBACK segments, but this is not generally used when you’re using automatic undo management.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 9 Using SQL in PL/SQL SQL Command Types: Query Locking Rows DECLARE CURSOR c IS CURSOR c IS SELECT * FROM a_table SELECT * FROM a_table FOR UPDATE [NOWAIT]; -- NOWAIT aborts for locked rows. FOR UPDATE [NOWAIT]; -- NOWAIT aborts for locked rows.BEGIN FOR i IN c LOOP FOR i IN c LOOP processing_statement; processing_statement; END LOOP; END LOOP;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 10 Using SQL in PL/SQL SQL Built-in Functions PL/SQL supports SQL built-in functions, and they can be used: PL/SQL supports SQL built-in functions, and they can be used: In SQL statements inside PL/SQL blocks. In SQL statements inside PL/SQL blocks. In PL/SQL statements and against PL/SQL variables. In PL/SQL statements and against PL/SQL variables. SQL built-in functions are qualified in the STANDARD package owned by the SYS user. SQL built-in functions are qualified in the STANDARD package owned by the SYS user. The STANDARD package addresses DATE, NUMBER and VARCHAR2 data types. The STANDARD package addresses DATE, NUMBER and VARCHAR2 data types. The DBMS_LOB package addresses LOB data types. The DBMS_LOB package addresses LOB data types.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 11 Using SQL in PL/SQL Pseudo Columns: ROWID and ROWNUM ROWID is a pseudo column that contains the physical block address to a row. ROWID is a pseudo column that contains the physical block address to a row. ROWNUM is a pseudo column that contains the number of rows processed by an explicit cursor, which is the number of rows, which are selected from a table. ROWNUM is a pseudo column that contains the number of rows processed by an explicit cursor, which is the number of rows, which are selected from a table. ROWNUM pseudo column can get Top-N SQL query results. ROWNUM pseudo column can get Top-N SQL query results.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 12 Using SQL in PL/SQL Pseudo Columns: SQL%ROWCOUNT SQL%ROWCOUNT is a cursor attribute that contains the number of rows processed by any SQL statement, like when you: SQL%ROWCOUNT is a cursor attribute that contains the number of rows processed by any SQL statement, like when you: Insert rows into a table. Insert rows into a table. Update rows in a table. Update rows in a table. Delete rows from a table. Delete rows from a table. Selecte rows from a table. Selecte rows from a table.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 13 Using SQL in PL/SQL Using Cursors All DML statements ( INSERT, UPDATE and DELETE ) inside PL/SQL blocks are implicit cursors. All DML statements ( INSERT, UPDATE and DELETE ) inside PL/SQL blocks are implicit cursors. All DQL statements are implicit or explicit cursors: All DQL statements are implicit or explicit cursors: Implicit DQL statements are not defined in the declaration section. Implicit DQL statements are not defined in the declaration section. Explicit DQL statements are defined in the declaration section. Explicit DQL statements are defined in the declaration section. Cursors are copies of stored data in private work areas. Cursors are copies of stored data in private work areas. All system reference cursors are explicit cursors that are: All system reference cursors are explicit cursors that are: Strongly typed, or reference a catalog object. Strongly typed, or reference a catalog object. Weakly typed, or do not reference a catalog object. Weakly typed, or do not reference a catalog object. Capable of being passed as parameters to subroutines. Capable of being passed as parameters to subroutines. Capable of being returned values from subroutines. Capable of being returned values from subroutines.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 14 Using SQL in PL/SQL Catalog Types: Implicit Cursor BEGIN FOR i IN (SELECT id, name FROM a_table) LOOP FOR i IN (SELECT id, name FROM a_table) LOOP dbms_output.put_line('ID: ['||i.id||']'); dbms_output.put_line('ID: ['||i.id||']'); dbms_output.put_line('Name: ['||i.name||']'); dbms_output.put_line('Name: ['||i.name||']'); END LOOP; END LOOP;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 15 Using SQL in PL/SQL Catalog Types: Explicit Cursor DECLARE CURSOR c (id_in NUMBER) IS CURSOR c (id_in NUMBER) IS SELECT id, name FROM a_table WHERE id = id_in; SELECT id, name FROM a_table WHERE id = id_in;BEGIN FOR i IN c LOOP FOR i IN c LOOP dbms_output.put_line('ID: ['||i.id||']'); dbms_output.put_line('ID: ['||i.id||']'); dbms_output.put_line('Name: ['||i.name||']'); dbms_output.put_line('Name: ['||i.name||']'); END LOOP; END LOOP;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 16 Using SQL in PL/SQL Catalog Types: Explicit Cursor DECLARE a_name VARCHAR2(10); a_name VARCHAR2(10); CURSOR c IS SELECT name FROM a_table; CURSOR c IS SELECT name FROM a_table;BEGIN OPEN c; OPEN c; LOOP LOOP FETCH c INTO a_number; FETCH c INTO a_number; EXIT WHEN c%NOTFOUND; EXIT WHEN c%NOTFOUND; dbms_output.put_line('Name: ['||a_name||']'); dbms_output.put_line('Name: ['||a_name||']'); END LOOP; END LOOP; CLOSE c; CLOSE c;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 17 Using SQL in PL/SQL Catalog Types: Strongly Typed Reference Cursor DECLARE TYPE strong_cursor IS REF CURSOR RETURN a_table%ROWTYPE; TYPE strong_cursor IS REF CURSOR RETURN a_table%ROWTYPE; cursor_variable STRONG_CURSOR; cursor_variable STRONG_CURSOR; row a_table%ROWTYPE; row a_table%ROWTYPE;BEGIN OPEN cursor_variable FOR OPEN cursor_variable FOR SELECT * FROM a_table; SELECT * FROM a_table; LOOP LOOP FETCH cursor_variable INTO row; FETCH cursor_variable INTO row; EXIT WHEN cursor_variable%NOTFOUND; EXIT WHEN cursor_variable%NOTFOUND; dbms_output.put_line('Print ['||row.name||']'); dbms_output.put_line('Print ['||row.name||']'); END LOOP; END LOOP; CLOSE cursor_variable; CLOSE cursor_variable;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 18 Using SQL in PL/SQL Catalog Types: Weakly Typed Reference Cursor DECLARE TYPE weak_cursor IS REF CURSOR; TYPE weak_cursor IS REF CURSOR; cursor_variable WEAK_CURSOR; cursor_variable WEAK_CURSOR; row a_table%ROWTYPE; row a_table%ROWTYPE;BEGIN OPEN cursor_variable FOR OPEN cursor_variable FOR SELECT * FROM a_table; SELECT * FROM a_table; LOOP LOOP FETCH cursor_variable INTO row; FETCH cursor_variable INTO row; EXIT WHEN cursor_variable%NOTFOUND; EXIT WHEN cursor_variable%NOTFOUND; dbms_output.put_line('Print ['||row.name||']'); dbms_output.put_line('Print ['||row.name||']'); END LOOP; END LOOP; CLOSE cursor_variable; CLOSE cursor_variable;END;/

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 19 Using SQL in PL/SQL Dynamic SQL Statements Dynamic SQL statements are executed as autonomous transactions, natively in a SQL*Plus subshell. Dynamic SQL statements are executed as autonomous transactions, natively in a SQL*Plus subshell. Dynamic SQL statements provide the means to run DDL statements in PL/SQL blocks, provided they don’t alter a table referenced in the same block. Dynamic SQL statements provide the means to run DDL statements in PL/SQL blocks, provided they don’t alter a table referenced in the same block. Dynamic SQL is also known as NDS, which stands for Native Dynamic SQL. Dynamic SQL is also known as NDS, which stands for Native Dynamic SQL.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 20 Using SQL in PL/SQL Regular Expressions REGEXP_LIKE() REGEXP_LIKE() Enables regular expression searches of character strings. Enables regular expression searches of character strings. REGEXP_INSTR() REGEXP_INSTR() Enables regular expression searches to locate a position in a string. Enables regular expression searches to locate a position in a string. REGEXP_REPLACE() REGEXP_REPLACE() Enables regular expression search and replace actions. Enables regular expression search and replace actions. REGEXP_SUBSTR() REGEXP_SUBSTR() Enables regular expression searches to locate a substring in a string. Enables regular expression searches to locate a substring in a string.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 21 Using SQL in PL/SQL Regular Expressions: Metacharacters *Matches zero or more characters. *Matches zero or more characters..A valid character..A valid character. ^Begins pattern matching from beginning of a line. ^Begins pattern matching from beginning of a line. []Groups characters, treats them as by a logical OR operation. []Groups characters, treats them as by a logical OR operation. $Ends pattern matching at the end of the line. $Ends pattern matching at the end of the line. \Escape character back quotes a special character, signaling it should be treated as an ordinary one. \Escape character back quotes a special character, signaling it should be treated as an ordinary one. ()Groups strings, which are delimited by a | symbol. ()Groups strings, which are delimited by a | symbol. \Escape character back quotes a special character, signaling it should be treated as an ordinary one. \Escape character back quotes a special character, signaling it should be treated as an ordinary one.

2006 Oracle Database PL/SQL 10g Programming (Chapter 4)Page 22 Summary Using SQL Statements Using SQL Statements Using SQL Built-in Functions Using SQL Built-in Functions Using Pseudo Columns Using Pseudo Columns Using Cursors Using Cursors Dynamic SQL Statements Dynamic SQL Statements Regular Expressions Regular Expressions