如何在 select 语句中插入 plsql 过程中存在的输入
How to insert a input present in plsql procedure in a select statement
我有 2 个 table,parent 和 child。我正在制作一个程序,它将 id 作为输入并给出两个 table 的输出。如果在 table 2 中找不到一行,它将给出一个异常。
为此,我为 select 语句使用了一个游标,并将其作为 out 参数传递到我的过程中,但我很困惑在游标的 select 语句中给出什么条件,以便它会检查作为输入提供的 id 并显示来自 tables.screen shot of my code
的行
代码:
create or replace procedure stu_proc
(st_id in student_main.stu_id%type,
st_cur out sys_refcursor)
is
cursor cur_st(st_id number) is select * from student_main sm
join student_details sd
on sm.id = sd.st_id
where sm.id = st_id;
st_id cur_st%rowtype;
begin
open cur_st;
loop
fetch cur_st into st_cur;
exit when cur_st%notfound;
end loop;
close cursor;
exception
when no_data_found then
dbms_output.put_line('Student details not found');
end;
你只需要将参数传递给游标:)
cursor cur_st(stud_id number) is
select * from student_main sm
join student_details sd
on sm.id = sd.st_id
where sm.id = stud_id;
并调用它
stu_proc(:stud_id, s_cur);
yes i can join but my main concern is to join only those rows whose
input will be provided by user as student id(used as IN parameter in
the procedure)
我发现您的代码几乎没有问题。
1) 未完成向游标传递参数,因此在评估游标时实际上未使用您传递的输入值。
2) 这个变量声明看起来不对 st_id cur_st%rowtype
。它应该像 st_cur cur_st%rowtype
。
另请注意,由于您已经在使用 s sys_refcursor
重新计算查询结果,因此无需打开另一个显式游标。您的代码可以修改如下:
请尝试以下。
CREATE OR REPLACE PROCEDURE stu_proc(
col_st_id IN student_main.id%type,
st_cur OUT sys_refcursor)
IS
BEGIN
OPEN st_cur for
SELECT sm.*
FROM student_main sm
JOIN student_details sd
ON sm.id = sd.st_id
WHERE sm.id = col_st_id;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Student details not found');
END;
我有 2 个 table,parent 和 child。我正在制作一个程序,它将 id 作为输入并给出两个 table 的输出。如果在 table 2 中找不到一行,它将给出一个异常。
为此,我为 select 语句使用了一个游标,并将其作为 out 参数传递到我的过程中,但我很困惑在游标的 select 语句中给出什么条件,以便它会检查作为输入提供的 id 并显示来自 tables.screen shot of my code
的行代码:
create or replace procedure stu_proc
(st_id in student_main.stu_id%type,
st_cur out sys_refcursor)
is
cursor cur_st(st_id number) is select * from student_main sm
join student_details sd
on sm.id = sd.st_id
where sm.id = st_id;
st_id cur_st%rowtype;
begin
open cur_st;
loop
fetch cur_st into st_cur;
exit when cur_st%notfound;
end loop;
close cursor;
exception
when no_data_found then
dbms_output.put_line('Student details not found');
end;
你只需要将参数传递给游标:)
cursor cur_st(stud_id number) is
select * from student_main sm
join student_details sd
on sm.id = sd.st_id
where sm.id = stud_id;
并调用它
stu_proc(:stud_id, s_cur);
yes i can join but my main concern is to join only those rows whose input will be provided by user as student id(used as IN parameter in the procedure)
我发现您的代码几乎没有问题。
1) 未完成向游标传递参数,因此在评估游标时实际上未使用您传递的输入值。
2) 这个变量声明看起来不对 st_id cur_st%rowtype
。它应该像 st_cur cur_st%rowtype
。
另请注意,由于您已经在使用 s sys_refcursor
重新计算查询结果,因此无需打开另一个显式游标。您的代码可以修改如下:
请尝试以下。
CREATE OR REPLACE PROCEDURE stu_proc(
col_st_id IN student_main.id%type,
st_cur OUT sys_refcursor)
IS
BEGIN
OPEN st_cur for
SELECT sm.*
FROM student_main sm
JOIN student_details sd
ON sm.id = sd.st_id
WHERE sm.id = col_st_id;
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('Student details not found');
END;