Sie sind auf Seite 1von 11

Chapter 2

Data Manipulation in PL/SQL

SQL Star International Ltd 2007

Objectives
At the end of this session, you will be able to :

Write SELECT statements in PL/SQL Perform Data Manipulations within PL/SQL blocks

2
SQL Star International Ltd 2007

Embedding SELECT statements


To interact with the database, a SELECT statement is used. SELECT statements within PL/SQL help retrieve data from the database. Syntax : SELECT <select list> INTO (variable_name [,variable_name] | record_name) FROM <table_name> WHERE condition;

3
SQL Star International Ltd 2007

Embedding SELECT Statements

(contd.)

Example to use the SELECT statement within a PL/SQL block:

SET SERVEROUT ON DECLARE vbName Book.cBookName%TYPE; vAuthor Book.cAuthorname%TYPE; BEGIN SELECT cBookName, cAuthorName INTO vbName,vAuthor FROM Book WHERE cBookID = HUM020000323; DBMS_OUTPUT.PUT_LINE( vbName||written by ||vAuthor); END;
SQL Star International Ltd 2007

Embedding SELECT statements

(contd.)

The main difference between a usual SELECT statement written in iSQL*Plus and a SELECT statement written in a PL/SQL block is the INTO clause. Even in embedded SELECT statements, a query must return only a single row. Output variable names and column names should be different to avoid ambiguity.

5
SQL Star International Ltd 2007

Embedding SELECT statements

(contd.)

Points to remember while writing SELECT statements within PL/SQL blocks :

Terminate every SQL statement with a semicolon. INTO clause is mandatory in a SELECT statement within the PL/SQL block. WHERE clause gives the condition to be satisfied for the data to be retrieved. The order and number of the output variables in the INTO clause must be same as that in the select list.

6
SQL Star International Ltd 2007

Embedding SELECT statements

(contd.)

Use %TYPE attribute to match the datatypes of the output variables and columns. You can use group functions in SELECT statement but not in the PL/SQL syntax.

7
SQL Star International Ltd 2007

DML Statements in PL/SQL

DML statements issued in SQL are sent to the Oracle server one at a time. DML statements issued within a PL/SQL block are all sent to the server at once for execution. This improves performance. COMMIT, SAVEPOINT and ROLLBACK define the beginning, breakpoint and end of a logical unit of a PL/SQL program.

8
SQL Star International Ltd 2007

DML statements in PL/SQL-Example1

Example for using DML statements in PL/SQL: BEGIN INSERT INTO Member VALUES(CAJ040501,ANN,JUDD,26A, Due Drops Apts, Blue Mountain Villa, Elizabeth,78099, NULL, 22, SYSDATE, C,N,04RANNJ); END;

9
SQL Star International Ltd 2007

DML statements in PL/SQL-Example2


Another example for using DML statements in a PL/SQL block: DECLARE vCopiesIncrease Book.nNoOfCopies%TYPE:=3; vMemID Member.cMemberID%TYPE := CDB028504; BEGIN UPDATE Book SET nNoOfCopies = nNoOfCopies + vCopiesIncrease WHERE cBookName = On The Street Where You Live; DELETE FROM Member WHERE cMemberID =vMemID; COMMIT; END;

10
SQL Star International Ltd 2007

Summary
In this session, we have discussed :

SELECT Statements in PL/SQL DML Statements in PL/SQL

11
SQL Star International Ltd 2007

Das könnte Ihnen auch gefallen