Oracle 从光标打印聚合函数的结果

Oracle 从光标打印聚合函数的结果,oracle,plsql,Oracle,Plsql,如何使用record变量检索游标实现中的count值 DECLARE CURSOR cur_branch IS SELECT b.branchname, COUNT(a.applicantid) FROM branch b, applicant a WHERE b.branchid=a.optedbranch GROUP BY b.branchname ORDER BY b.branchname; BEGIN DBMS_OUTPUT.

如何使用record变量检索游标实现中的count值

DECLARE 

   CURSOR cur_branch IS
    SELECT b.branchname, COUNT(a.applicantid) 
      FROM branch b, applicant a
     WHERE b.branchid=a.optedbranch
     GROUP BY b.branchname ORDER BY b.branchname;

BEGIN

   DBMS_OUTPUT.PUT_LINE('Branch Name No of Applicants opted');

   FOR v_branchrec IN cur_branch
   LOOP

      DBMS_OUTPUT.PUT(RPAD(v_branchrec.branchname,20)||'    '); 
      DBMS_OUTPUT.PUT_LINE(v_branchrec.COUNT(applicantid));

   END LOOP;

END;
这会在
DBMS_OUTPUT.PUT_行(v_branchrec.COUNT(applicationId))抛出一个错误声明必须声明计数,并且只能在SQL中使用,不能在PL/SQL中使用


我想检索每个分支的学生人数。

您应该为
COUNT(a.applicationId)
定义一个别名,并通过该别名引用此列

 SELECT b.branchname, COUNT(a.applicantid) cnt FROM
...
 DBMS_OUTPUT.PUT_LINE(v_branchrec.cnt);
您需要做的只是:

  • 为光标定义中的COUNT(a.applicationId)列使用别名:

      SELECT b.branchname, COUNT(a.applicantid) as cnt
    
  • 循环中
    使用

      DBMS_OUTPUT.PUT_LINE(v_branchrec.cnt);