1

我正在尝试从我的数据库中获取数据,但在我的代码中找不到错误:

declare
  cursor c1 is select * from(select d.department_ID, d.department_name,count(d.department_name) cnt from emp e,dept d where e.department_id=d.department_id group by d.department_ID, d.department_name) where cnt>=5;
  TYPE cust_dept IS RECORD(dept_id dept.department_id%type,dept_name dept.department_name%type,emp_name emp.first_name%type);

begin 

FOR i IN c1
loop

    select * into cust_dept  from emp e, dept d where d.department_ID=i.department_ID;

 DBMS_OUTPUT.put_line('Department_id :'||i.department_id||' '||'department_name :'||i.department_name);--||' '||'customer_name :'||cust_dept.emp_name);
    --FETCH customer_cur into customer_rec;
    --EXIT WHEN customer_cur%notfound;
    --DBMS_OUTPUT.put_line(i.department_id);

 END LOOP;

end;
/

错误是:

ORA-06550: line 10, column 23:
PLS-00321: expression 'CUST_DEPT' is inappropriate as the left hand side of an assignment statement
ORA-06550: line 10, column 34:
PL/SQL: ORA-00904: : invalid identifier
ORA-06550: line 10, column 9:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
4

2 回答 2

1

您的代码有几个问题

  1. cust_dept 是一个类型而不是一个变量来声明一个 cust_dept 类型的变量你需要这样的东西: cust_dept_var cust_dept;
  2. cust_dept 变量的定义不会解决您的问题,因为select * from emp e, dept d where d.department_ID = i.department_ID可能不会返回单个答案,可能有 2 个或更多或没有结果,所以基本上您需要定义 cust_dept 的数组变量
  3. 要得到您需要的结果,您可以使用下面的代码,您不需要定义记录类型,否则:

    begin
    for x in
    (select d.department_ID, d.department_name from emp e, dept d where e.department_id = d.department_id group by d.department_ID, d.department_name having count(d.department_name) >= 5) loop

    for y in (select e.department_id, d.department_name from emp e, dept d where d.department_ID = x.department_ID) loop DBMS_OUTPUT.put_line('Department_id :' || y.department_id || ' ' || 'department_name :' || y.department_name);
    end loop;
    end loop;
    end;

  4. 祝你好运 ;)
于 2013-02-05T13:29:30.210 回答
0

您应该声明类型的变量cust_dept并选择它。

于 2013-02-05T07:23:59.287 回答