使用存储过程传入​​字符串时如何输出游标?

时间:2010-11-27 23:46:36

标签: sql oracle stored-procedures

嘿伙计们,我正在处理一个存储过程,但我仍然坚持如何输出下面的光标。我想让它接受我给它的任何课程,并吐出计数,课程编号和课程描述。这是我到目前为止所做的:

EXEC STUDENT_COUNT_COURSE('CS 101');

CALLS

CREATE OR REPLACE PROCEDURE STUDENT_COUNT_COURSE (p_CrsNum IN COURSE.COURSENUM%TYPE)
IS
  cursor cursor1 IS
          SELECT     CourseDesc.courseNum, CourseDesc.courseDesc, COUNT(DISTINCT Student.studentID) as "Count",
          FROM         Course INNER JOIN
                      CourseDesc ON Course.courseNum = CourseDesc.courseNum INNER JOIN
                      Grades ON Course.courseID = Grades.courseID INNER JOIN
                      Student ON Grades.studentID = Student.studentID
          WHERE     (CourseDesc.courseNum = p_CrsNum)
          GROUP BY CourseDesc.courseNum, CourseDesc.courseDesc;
begin          
for c in cursor1
loop
dbms_output.putline('There are COUNT students in' || c.courseNum ||', '|| c.dourseDesc);
//still working on count...
end loop;
end;

3 个答案:

答案 0 :(得分:1)

您需要一个游标变量来保存结果集,您可以将其作为过程OUT参数输出,但通过函数返回它是处理事物的常用方法。像这样:

CREATE OR REPLACE FUNCTION STUDENT_COUNT_COURSE 
    (p_CrsNum IN COURSE.COURSENUM%TYPE)
    RETURN sys_refcursor
IS
    rc sys_refcursor;
BEGIN
    open rc for 
          SELECT     CourseDesc.courseNum
                     , CourseDesc.courseDesc
                     , COUNT(DISTINCT Student.studentID)
          FROM         Course INNER JOIN
                      CourseDesc ON Course.courseNum = CourseDesc.courseNum INNER JOIN
                      Grades ON Course.courseID = Grades.courseID INNER JOIN
                      Student ON Grades.studentID = Student.studentID
          WHERE     (CourseDesc.courseNum = p_CrsNum)
          GROUP BY CourseDesc.courseNum, CourseDesc.courseDesc;

    return rc;
end; 

答案 1 :(得分:0)

我认为您可能需要的是这样的(只需读取光标并将其发送到标准输出)

LOOP 
 FETCH cursor1
  INTO  couse_num, course_name, num_students;
EXIT WHEN v_cursor%NOTFOUND;
 DBMS_OUTPUT.PUT_LINE(couse_num|| ' , ' || course_name|| ' , ' || num_students);
END LOOP;
CLOSE cursor1;

但首先你必须创建游标(由APC发布的方式)

答案 2 :(得分:0)

我觉得很愚蠢......我的dbms_output.put_line缺少一个下划线,并且在我的select语句末尾有一个额外的逗号。

以下是我使用的解决方案:

CREATE OR REPLACE PROCEDURE STUDENT_COUNT_COURSE(p_CrsNum IN COURSE.COURSENUM%TYPE)
AS
  cursor cursor1 is SELECT CourseDesc.courseNum, 
                    CourseDesc.courseDesc, 
                    COUNT(DISTINCT Student.studentID) as "Count"
          FROM      Course INNER JOIN
                    CourseDesc ON Course.courseNum = CourseDesc.courseNum INNER JOIN
                    Grades ON Course.courseID = Grades.courseID INNER JOIN
                    Student ON Grades.studentID = Student.studentID
          WHERE     (CourseDesc.courseNum = p_CrsNum)
          GROUP BY CourseDesc.courseNum, CourseDesc.courseDesc;
BEGIN          
    FOR c IN cursor1
    LOOP
        IF C."Count"=1 THEN
           DBMS_OUTPUT.PUT_LINE('There is '||c."Count"||' student in ' || c.courseNum ||', '||c.CourseDesc);
        ELSE
            DBMS_OUTPUT.PUT_LINE('There are '||c."Count"||' students in ' || c.courseNum ||', '||c.CourseDesc);
        END IF;
    END LOOP;
END;