你必须选择进入的东西。如果您不这样做,则查询 isn't even executed(尽管已解析)。
create or replace procedure select_procedure
as
l_name student.name%TYPE;
l_surname student.name%TYPE;
begin
execute immediate
'select name, surname
from student
where id_student = 1'
into l_name, l_surname;
end;
/
但是,没有特定的顺序: (a) 您应该使用绑定变量,而不是在动态语句中嵌入文字值 1; (b) 这根本不需要是动态的; (c) 调用者无论如何都无法看到查询返回的值 - 除非您改为选择 OUT 参数,或者使用 dbms_output() 显示它们(尽管这实际上应该只用于调试无法控制客户端是否显示)。
所以你可以这样做:
create or replace procedure select_procedure
as
l_name student.name%TYPE;
l_surname student.name%TYPE;
begin
select name, surname
into l_name, l_surname
from student
where id_student = 1;
dbms_output.put_line('name=' || l_name ||', surname=' || l_surname);
end;
/
或
create or replace procedure select_procedure (
p_name OUT student.name%TYPE,
p_surname OUT student.name%TYPE
)
as
begin
select name, surname
into p_name, p_surname
from student
where id_student = 1;
end;
/
并让你的调用者传入它自己的变量名来填充,然后对它们做任何需要的事情。调用者通常还会传入您要查找的 ID,因此您没有硬编码的 1。
不过,过程似乎并不是最好的机制。
此外,如果查询返回零行或多行,使用select ... into(静态或动态)将出错。仅当返回一行时才有效。游标可以处理任意数量的行 - 但除非您只是打印结果(如@Jayanth 所示),否则您需要将游标传回给调用者。您可以改为使用bulk collect into 集合,但您仍然需要对此进行处理。