0

我正在写下面的代码。将过程 IN 参数与光标值进行比较。

create or replace procedure  dashboard_addtion(customer_name IN varchar2)
IS
num number:=0;
CURSOR cname_cur 
    IS Select customername from customer_master;
cname varchar2(300);
begin
    for cname in cname_cur loop
        if upper(cname.customername)!=upper(customer_name) then
            num:=num+1;
        end if;
    end loop;
    if num != 0 then 
        insert into customer_master select max(customerid) + 1, customer_name
        from customer_master;
    end if;
end;

一直在执行 INSERT 语句。

4

1 回答 1

0

在您明确想要尝试使用游标的基础上......您似乎有num递增的逻辑并向后检查:

for cname in cname_cur loop
  if upper(cname.customername) = upper(customer_name) then
    num := num+1;
  end if;
end loop;
if num = 0 then 
  insert into customer_master
  select max(customerid)+1,customer_name from customer_master;
end if;

这将计算与您的参数匹配的游标记录,并且只有在没有找到时才进行插入。

boolean使用标志可能更清楚:

... is
  found boolean := false;
  cursor cname_cur is select custname from customer_master;
begin
  for cname in cname_cur loop
    if upper(cname.customername) = upper(customer_name) then
      found := true;
    end if;
  end loop;
  if not found then
    insert into customer_master
    select max(customerid)+1,customer_name from customer_master;
  end if;
end;

另请注意,您不需要cname显式声明;它被重新声明为for ... in ... loop语法的一部分。

于 2013-06-26T12:19:13.473 回答