0% found this document useful (0 votes)
48 views

PLSQL 3 3 SG

The document discusses various SQL data manipulation language (DML) statements that can be used within PL/SQL blocks, including INSERT, UPDATE, DELETE, and MERGE. It also covers implicit and explicit cursors. Implicit cursors are automatically created by Oracle for each DML statement, while explicit cursors must be declared by the programmer to work with result sets containing multiple rows. Attributes like SQL%ROWCOUNT can provide information about the results of a DML statement.

Uploaded by

DV D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

PLSQL 3 3 SG

The document discusses various SQL data manipulation language (DML) statements that can be used within PL/SQL blocks, including INSERT, UPDATE, DELETE, and MERGE. It also covers implicit and explicit cursors. Implicit cursors are automatically created by Oracle for each DML statement, while explicit cursors must be declared by the programmer to work with result sets containing multiple rows. Attributes like SQL%ROWCOUNT can provide information about the results of a DML statement.

Uploaded by

DV D
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Release Date: August, 2016

Updates:
2
3
4
5
A DML statement within a PL/SQL block can modify many rows (unlike a SELECT statement, which must
read exactly one row).

6
7
8
Important Note: The data in the EMPLOYEES table needs to remain unchanged for later in the course. Therefore we
use the COPY_EMP table in all these DML examples.

If you haven't already created the COPY_EMP table, do so now by executing this SQL statement:

CREATE TABLE copy_emp


AS SELECT *
FROM employees;

In the example in the slide, an INSERT statement is used within a PL/SQL block to insert a row into the COPY_EMP
table.

While using the INSERT command in a PL/SQL block, you can:

Use SQL functions, such as USER and SYSDATE.


Generate primary key values by using existing database sequences.
Derive values in the PL/SQL block.

9
There may be ambiguity in the SET clause of the UPDATE statement because although the identifier on the
left of the assignment operator is always a database column, the identifier on the right can be either a
database column or a PL/SQL variable. Recall that if column names and identifier names are identical in the
WHERE clause, then the Oracle server looks to the database first for the name.

Remember that the WHERE clause is used to determine which rows are affected. If no rows are modified,
then no error occurs (unlike the SELECT statement in PL/SQL). If the WHERE clause is not used, then all the
rows in a table will be removed, provided that no integrity constraints are violated.

PL/SQL variable assignments always use :=, and SQL column assignments always use =.

Although we are using the copy_emp table, it is ok to use the original table for the definition of the variable
datatype employees.salary%TYPE.

10
The DELETE statement removes unwanted rows from a table. If the WHERE clause is not used, then all the
rows in a table will be removed, provided that no integrity constraints are violated.

11
The MERGE statement inserts or updates rows in one table by using data from another table. Each row is
inserted or updated in the target table, depending on an equijoin condition.

The example shown matches the employee_id in the COPY_EMP table to the employee_id in the
EMPLOYEES table. If a match is found, then the row is updated to match the row in the EMPLOYEES table. If
the row is not found, then it is inserted into the copy_emp table.

12
13
The word “cursor” has several meanings in Oracle. It is sometimes used to mean a pointer to the private
memory area, rather than the memory area itself. It is also used to refer to an area of shared memory. In
thıs course, we focus only on its meaning in the PL/SQL environment.

14
Implicit cursors: Implicit cursors are created and managed by the Oracle server. Oracle uses an implicit
cursor for each SELECT, UPDATE, DELETE, or INSERT statement you execute in a program. You do not have
access to them. The Oracle server creates such a cursor when it has to execute a SQL statement. The
remaining pages in this lesson discuss implicit cursors.

Explicit cursors: Explicit cursors are declared by the programmer. As a programmer you may want to
retrieve multiple rows from a database table, have a pointer to each row that is retrieved, and work on the
rows one at a time. In such cases, you can declare cursors explicitly depending on your business
requirements. You declare these cursors in the declarative section of a PL/SQL block. Explicit cursors are
discussed in a later lesson.

15
You can test the attributes — SQL%ROWCOUNT, SQL%FOUND, and SQL%NOTFOUND — in the executable
section of a block to gather information after the appropriate command. PL/SQL does not return an error if
a DML statement does not affect any rows in the underlying table. However, if a SELECT statement does
not retrieve any rows, PL/SQL returns an exception.

Observe that the attributes are prefixed with the automatic name of the implicit cursor: “SQL.”

The SQL%NOTFOUND attribute is opposite to SQL%FOUND. This attribute may be used as the exit condition
in a loop. It is useful in UPDATE or DELETE statements when no rows are changed because exceptions are
not returned in these cases.

You will learn about explicit cursor attributes later in the course.

16
17
The example in the slide deletes all rows with department_id 50 from the copy_emp table. Using the
SQL%ROWCOUNT attribute, you can display the number of rows deleted.

18
19
Since every embedded SQL statement creates an implicit cursor, and all implicit cursors are called “SQL," each SQL statement in
turn will result in its own values in the cursor attributes. Therefore the SQL%ROWCOUNT value returned from the UPDATE
statement will be overwritten by the INSERT statement. The goal of the code in the slide is to store the number of rows updated by
the UPDATE statement in the RESULTS table. What actually happens, though, is an error. Upon starting the INSERT statement, the
previous cursor attributes are "erased." Until the INSERT statement completes, there are no new cursor attributes, so there is no
value to be inserted into the RESULTS table.

To use a previous implicit cursor attribute in a later SQL statement, you must save the value in an explicitly declared variable
before it is "erased" by the start of a new SQL statement. See the variable V_ROWCOUNT in the code below. Note also the
presence of the PUT_LINE statements that will show you the value in SQL%ROWCOUNT after the UPDATE statement and again
after the INSERT statement. Using PUT_LINEs in this way is an excellent debugging technique.

DECLARE
v_rowcount INTEGER;
BEGIN UPDATE copy_emp SET salary = salary + 100 WHERE job_id = 'ST_CLERK';
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows in COPY_EMP updated.');
v_rowcount := SQL%ROWCOUNT; -- not a SQL statement, does not alter SQL%ROWCOUNT
INSERT INTO results (num_rows)
VALUES (v_rowcount);
DBMS_OUTPUT.PUT_LINE(SQL%ROWCOUNT || ' rows in RESULTS updated.');
END;

20
INSERT - Statement adds new rows to the table.
UPDATE - Statement modifies existing rows in the table.
DELETE - Statement removes rows from the table.
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.
Explicit cursors: Defined by the programmer for queries that return more than one row.
Implicit cursors: Defined automatically by Oracle for all SQL data manipulation statements, and for queries
that return only one row.

21
22

You might also like