将过程参数与游标值进行比较

时间:2013-06-26 11:28:54

标签: plsql oracle11g oracle10g plsqldeveloper

我正在编写下面的代码。将过程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语句。

1 个答案:

答案 0 :(得分: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;

这将计算与您的参数匹配的光标记录,并且仅在找到 none 时才进行插入。

使用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语法的一部分。