首页 > 解决方案 > 在 PL/SQL 中向表中插入值

问题描述

由于这个原因,我是 PL/SQL 的新手,很抱歉这个问题很简单。我希望你能帮助我。

我的目标是使用 for 循环向表中添加值。如果您有任何问题,请重播,我会尽快回答。这是我的代码不起作用。

declare
    cursor c_exam_info is select result1 from exam_info;
begin
    for i IN c_exam_info
    loop 
    if result1>60 then 
    insert into exam_info(result1) values('pass');
    else 
    insert into exam_info(result1) values('fail');
    end if;
    end loop;
end;

这是我的桌子

标签: oracleplsql

解决方案


我无法查看图像。

不管怎样:INSERT似乎不合适——不是UPDATE更好的选择吗?像这样的东西:

begin
    for i IN (select id, result1 from exam_info) loop
      update exam_info set
        result_type = case when c_exam_info.result1 > 60 then 'pass'
                           else 'fail'
                      end
        where id = c_exam_info.id;
    end loop;
end;

虽然,同样可以在 SQL 层中完成,但您不需要 PL/SQL(除非它是家庭作业,所以您正在练习 PL/SQL)。

update exam_info set
  result_type = case when c_exam_info.result1 > 60 then 'pass'
                     else 'fail'
                end;

result1如果该列 ( ) 是一个数字(例如 60),则用字符串(“失败”或“通过”)更新(甚至插入)同一列没有多大意义。另外,它是什么样的表——只有一列;这样有什么好处INSERT(你在代码中写的那个)?


推荐阅读