Beruflich Dokumente
Kultur Dokumente
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
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
(contd.)
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
(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
(contd.)
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
(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 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
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
10
SQL Star International Ltd 2007
Summary
In this session, we have discussed :
11
SQL Star International Ltd 2007