Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Using SQL in PL/SQL Oracle Database PL/SQL 10g Programming Chapter 4."— Presentation transcript:

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

2 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

3 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.

4 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.

5 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.

6 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;/

7 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;/

8 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.

9 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;/

10 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.

11 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.

12 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.

13 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.

14 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;/

15 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;/

16 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;/

17 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;/

18 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;/

19 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.

20 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.

21 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.

22 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


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

Similar presentations


Ads by Google