日期:2014-05-17  浏览次数:20937 次

重新发个。请问这个pl sql代码哪错了,应该怎么修改。
之前发的那个帖子漏洞很多,可能是写糊涂了吧。
这里重新发一个。
set serveroutput on
declare
cursor c_kk is
select name
from s_customer
where s_customer.region_id=s_dept.region_id
and region_id=(select region_id from s_dept where name='peter');
ckk s_customer.name%type;
begin
open c_kk;
loop
fetch c_kk into ckk;
exit when c_kk%notfound;
dbms_output.put_line(ckk);
end loop;
close c_kk;
end;
/

这是我写的一个pl sql代码。用动态光标写。
有两个表,s_dept和s_customer。
他们有一个相同的项region_id。
现在我想打印s_customer表里的name项,条件是s_dept表中的name叫peter。
然后就出错了。不知道是为什么。

这是出错的报告。

where s_customer.region_id=s_dept.region_id
                           *
ERROR at line 5:
ORA-06550: line 5, column 28:
PL/SQL: ORA-00904: "S_DEPT"."REGION_ID": invalid identifier
ORA-06550: line 3, column 1:
PL/SQL: SQL Statement ignored

------解决方案--------------------
select name
from s_customer
where s_customer.region_id=s_dept.region_id
and region_id=(select region_id from s_dept where name='peter');
这个SQL有问题吧,from一个表,但是where中却是用了s_dept.region_id
改为:
select s_customer.name
from s_customer, s_dept
where s_customer.region_id=s_dept.region_id
and s_customer.region_id=(select region_id from s_dept where name='peter');