之前发的那个帖子漏洞很多,可能是写糊涂了吧。
这里重新发一个。
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

解决方案 »

  1.   

    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');
      

  2.   


    还是有问题。
    现在变成单行查询返回多行了。
    set serveroutput on
    declare
    cursor c_kk is
    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='Sales');
    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;
    /declare
    *
    ERROR at line 1:
    ORA-01427: single-row subquery returns more than one row
    ORA-06512: at line 11
      

  3.   

    写错了,第7行应该是 and s_customer.region_id=(select region_id from s_dept where name='peter');
    跟上一个代码弄串了。
      

  4.   

    感觉你没有进行联合查询,你的意思是不是:
    select name
    from s_customer
    where region_id=(select region_id from s_dept where name='Sales');
    这样就可以了?
      

  5.   


    意思是这个意思,我之前这么着写过,得到的结果跟2楼的一样,依然是single-row subquery returns more than one row ,所以后来就改联合查询了。
      

  6.   

    那看一下子查询
    (select region_id from s_dept where name='Sales');
    中,返回是否是一行?
    如果是多行,那么where region_id=(多行) 就会报错;除非改为where region_id in (多行)
      

  7.   

    或者把子查询限制为一行:加where rownum = 1;
    但是这样你的表结构设计或者数据本身是有问题的
      

  8.   

    我自己解决了。 把等号换成in就可以显示了。
    就是
    select name
    from s_customer
    where region_id in(select region_id from s_dept where name='peter');非常感谢您一直回复!马上给您分!
      

  9.   


    set serveroutput on
    declare cursor c_kk is
    select cust.name
      from s_customer cust
          ,s_dept     dept
     where cust.region_id = dept.region_id 
      and dept.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; /