Plsql 将过程参数与光标值进行比较
我正在写下面的代码。将参数中的过程与CURSOR值进行比较Plsql 将过程参数与光标值进行比较,plsql,oracle11g,oracle10g,plsqldeveloper,Plsql,Oracle11g,Oracle10g,Plsqldeveloper,我正在写下面的代码。将参数中的过程与CURSOR值进行比较 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
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语句。基于您明确希望使用游标进行实验的基础。。。您似乎拥有
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;
这将统计与您的参数匹配的游标记录,并且仅在未找到任何游标记录时进行插入
使用布尔
标志可能更清晰:
... 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
;它被重新声明为。。。在里面循环语法。标题似乎与问题无关,尽管我不完全确定问题是什么。您正在遍历所有现有记录,并计算那些与您的参数不匹配的记录;如果有不匹配的记录,则插入新记录。是否仅当与参数匹配的记录不存在时才尝试插入新记录?您不需要光标。@AlexPoole:是的,Alex我正在尝试插入新记录,但前提是它已经不存在。@AlexPoole:是的,我知道不使用光标也可以插入新记录。但我只是想清楚地了解游标的工作原理,请建议TY:)这里有很多变量名混淆,以及过程名拼写错误——所有这些都会让你和同事在真实的开发环境中发疯。将PL/SQL变量命名为与它们等效的数据库列相同的名称,并在SQL语句中使用PL/SQL块名称(例如“customer\u master.customer\u Name=dashboard\u addition.customer\u Name”)。谢谢,我可能需要您的更多帮助来熟悉PL/SQL:)