Is there a way to declare a variable with a select statement? Below is an example.
Declare
P_Price orders.p_price%type;
Begin
P_Price := (select price from products where p_id = 1000);
Look up price in a table to declare the price
You declare a variable in the declaration section (between declare
and begin
in an anonymous PL/SQL block). You seem to be asking about assigning a value to that variable. You do that with a select into
.
select price
into p_price
from products
where p_id = 1000;
If the query returns anything other than 1 row, you'll get an exception. no_data_found
if the query returns 0 rows and too_many_rows
if the query returns more than 1 row.
If you want the full anonymous block (including the declaration section that you already had unchanged)
declare
p_price products.price%type;
begin
select price
into p_price
from products
where p_id = 1000;
end;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.