为什么我的显式游标仅从 PL/SQL 中的数据库中提取特定行?
Why does my explicit cursor fetch only specific rows from my database in PL/SQL?
我是 PL/SQL 的新手,我正在尝试使用显式游标遍历我的数据库 FLEX_PANEL_INSPECTIONS。我想使用显式游标依次从数据库中获取每一行,并根据行中给定 'panel' 的随机生成 'status',在 if 中为面板分配一个新的状态值/else 语句。面板的状态是随机的但布尔值 - 它是 1 或 0。
但是,当我查看 DBMS 输出时,我注意到提取不会从数据库中检索所有值 - 只有那些状态值为 1 的值。我在下面包含了核心代码。
如果有人能够帮助我找到解决方案,或者解释我的问题的根本原因,我将不胜感激,谢谢!
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
old_panel_status number;
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
open panel_cursor;
loop
fetch panel_cursor into old_panel_status;
exit when panel_cursor%notfound;
if old_panel_status = 0
then new_panel_status := 2;
elsif old_panel_status = 1
then new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question).
dbms_output.put_line(old_panel_status);
--Test output
--This displays all of the 1's that were randomly generated in the original table.
--It does not display any of the 0's that were generated.
end if;
end loop;
close panel_cursor;
close sensor_cursor;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
如果您在删除额外的 elseif 子句时没有犯错,那么问题出在您的 dbms_output.put_line。
它位于else部分内部,因此只有在调用该子句时才会触发。将它移到 END IF 下方并确保使用正确的缩进,这样更容易发现这些东西。
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
old_panel_status number;
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
open panel_cursor;
loop
fetch panel_cursor into old_panel_status;
exit when panel_cursor%notfound;
if old_panel_status = 0
then new_panel_status := 2;
elsif old_panel_status = 1
then new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question)
end if;
dbms_output.put_line(old_panel_status);
end loop;
close panel_cursor;
close sensor_cursor;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
除了已经在接受的答案中修复的主要错误之外,下面的代码还显示了更新的循环结构。您可以选择您喜欢的任何变量名称,而不是 rec
。在每次迭代中,它包含一行(通常不止一列)。
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
for rec in panel_cursor loop
if rec.flex_panel_status = 0 then
new_panel_status := 2;
elsif rec.flex_panel_status = 1 then
new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question)
end if;
dbms_output.put_line(rec.flex_panel_status);
end loop;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
如果你愿意,你甚至可以摆脱显式光标:
for rec in (
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS
) loop
我是 PL/SQL 的新手,我正在尝试使用显式游标遍历我的数据库 FLEX_PANEL_INSPECTIONS。我想使用显式游标依次从数据库中获取每一行,并根据行中给定 'panel' 的随机生成 'status',在 if 中为面板分配一个新的状态值/else 语句。面板的状态是随机的但布尔值 - 它是 1 或 0。 但是,当我查看 DBMS 输出时,我注意到提取不会从数据库中检索所有值 - 只有那些状态值为 1 的值。我在下面包含了核心代码。 如果有人能够帮助我找到解决方案,或者解释我的问题的根本原因,我将不胜感激,谢谢!
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
old_panel_status number;
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
open panel_cursor;
loop
fetch panel_cursor into old_panel_status;
exit when panel_cursor%notfound;
if old_panel_status = 0
then new_panel_status := 2;
elsif old_panel_status = 1
then new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question).
dbms_output.put_line(old_panel_status);
--Test output
--This displays all of the 1's that were randomly generated in the original table.
--It does not display any of the 0's that were generated.
end if;
end loop;
close panel_cursor;
close sensor_cursor;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
如果您在删除额外的 elseif 子句时没有犯错,那么问题出在您的 dbms_output.put_line。
它位于else部分内部,因此只有在调用该子句时才会触发。将它移到 END IF 下方并确保使用正确的缩进,这样更容易发现这些东西。
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
old_panel_status number;
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
open panel_cursor;
loop
fetch panel_cursor into old_panel_status;
exit when panel_cursor%notfound;
if old_panel_status = 0
then new_panel_status := 2;
elsif old_panel_status = 1
then new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question)
end if;
dbms_output.put_line(old_panel_status);
end loop;
close panel_cursor;
close sensor_cursor;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
除了已经在接受的答案中修复的主要错误之外,下面的代码还显示了更新的循环结构。您可以选择您喜欢的任何变量名称,而不是 rec
。在每次迭代中,它包含一行(通常不止一列)。
create or replace procedure FLEX_SUMMARY_STATUS_PROCEDURE as
new_panel_status number;
cursor panel_cursor is
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS;
begin
for rec in panel_cursor loop
if rec.flex_panel_status = 0 then
new_panel_status := 2;
elsif rec.flex_panel_status = 1 then
new_panel_status := 3;
--More conditional loops follow (but are irrelevant for this question)
end if;
dbms_output.put_line(rec.flex_panel_status);
end loop;
end FLEX_SUMMARY_STATUS_PROCEDURE;
/
如果你愿意,你甚至可以摆脱显式光标:
for rec in (
select FLEX_PANEL_STATUS
from FLEX_PANEL_INSPECTIONS
) loop