PL SQL7
PL SQL7
In this syntax, the number of columns in the variable_list must be the same as the
number of
variables (or the number of components of a record) in the select_list . In
addition, their
corresponding data type must be compatible.
Besides the WHERE clause, you can use other clauses in the SELECT statement such as
INNER JOIN ,
GROUP BY , HAVING , and UNION .
If the SELECT statement returns more than one row, Oracle will raise the
TOO_MANY_ROWS exception. If
the SELECT statement does not return any row, Oracle will raise the NO_DATA_FOUND
exception.
Let’s use the customers and contacts tables in the sample database for
demonstration.
SELECT
select_list
INTO
variable_list
FROM
table_name
WHERE
condition;
In this example:
First, declare a variable l_customer_name whose data type anchors to the name
columns of the
customers table. This variable will hold the customer name.
Second, use the SELECT INTO statement to select value from the name column and
assign it to
the l_customer_name variable.
Third, show the customer name using the dbms_output.put_line procedure.
Because the customers table has only one row with customer ID 100, the code block
displayed the
customer name.
DECLARE
l_customer_name customers.name%TYPE;
BEGIN
-- get name of the customer 100 and assign it to l_customer_name
SELECT name INTO l_customer_name
FROM customers
WHERE customer_id = 100;
-- show the customer name
dbms_output.put_line( v_customer_name );
END;
Verizon
If there were no such row, the code block would fail with an unhandled
NO_DATA_FOUND exception.
In this example:
First, declare a record based on the row of the customers table. This record will
hold the entire
row of the customers table.
Second, select the customer whose id is 100 into the r_customer record.
Third, show the customer’s name and website.
DECLARE
l_customer_name customers.name%TYPE;
In this example:
If the number of columns and expression in the SELECT clause is greater than the
number of variables
in the INTO clause, Oracle issues this error:
Oracle issues the following error if the number of columns and expression in the
SELECT clause is less
than the number of variables in the INTO clause:
If the number of variables and element in the select list are the same, but their
corresponding
datatypes are not compatible so that Oracle cannot implicitly convert from one type
to the other. It will
issue the following error:
Now, you should know how to use the PL/SQL SELECT INTO statement to fetch a single
row from a
table into variables.