PL/SQL
 
Simple Programs in PL/SQL
 
The simplest form of program has some declarations followed by an executable section consisting of one or more of the SQL statements with which we are familiar. The major nuance is that the form of the SELECT statement is different from its SQL form. After the SELECT clause, we must have an INTO clause listing variables, one for each attribute in the SELECT clause, into which the components of the retrieved tuple must be placed.
 
 
Notice we said "tuple" rather than "tuples", since the SELECT statement in PL/SQL only works if the result of the query contains a single tuple. The situation is essentially the same as that of the "single-row select" discussed in Section 7.1.5 of the text, in connection with embedded SQL. If the query returns more than one tuple, you need to use a cursor, as described in the next section. Here is an example:
 
 
CREATE TABLE T1(

e INTEGER,

f INTEGER

);
 
 
DELETE FROM T1;

INSERT INTO T1 VALUES(1, 3);

INSERT INTO T1 VALUES(2, 4);
 
/* Above is plain SQL; below is the PL/SQL program. */
 
 
DECLARE
a NUMBER;

b NUMBER;
 
 
BEGIN
SELECT e,f INTO a,b FROM T1 WHERE e>1;
 
INSERT INTO T1 VALUES(b,a);
 
 
END;
.
 
 
run;
Fortuitously, there is only one tuple of T1 that has first component greater than 1, namely (2,4). The INSERT statement thus inserts (4,2) into T1.