Oracle中的函数,它返回表的2个不同列

时间:2019-07-05 01:27:16

标签: oracle plsql oracle11g stored-functions

我正在尝试创建一个在单个表中返回2个不同列的函数,有人可以帮我吗?

我已经尝试过这种方式:

CREATE FUNCTION return_id_grade(subjectId IN NUMBER, semesterYear IN DATE , n IN INT, option IN INT)
RETURN NUMBER
                IS studentId NUMBER(5),
                IS studentGrade NUMBER(2,1);
   BEGIN
                SELECT DISTINCT student_id INTO studentId,
                       grade INTO studentGrade
                    FROM (SELECT studentId, grade, dense_rank() over (ORDER BY grade desc) rank FROM old_students) 
                        WHERE subject_id = subjectId 
                            AND semester_year = semesterYear
                            AND rank = n
                            AND rownum <= 1
    CASE 
                WHEN option = 1 then RETURN(student_id)
                WHEN option = 2 then RETURN(grade)
    END;

END;

我希望输出一个大学班级的n'NTH成绩和学生ID,但是实际情况只能输出在参数字段上收到的选项。

3 个答案:

答案 0 :(得分:1)

a。您不能使用Select colum1 INTO variable1,colum2 INTO variable2。它必须像: 选择column1,column2 INTO变量1,变量2

b。创建对象类型并将其用作过程中的out参数

c。在调用该过程之后具有选项条件。

示例代码:

      CREATE OR REPLACE TYPE ty_obj_idgrade AS OBJECT
      (studentId    NUMBER(5)
      ,studentGrade    NUMBER(2,1)
       );



      CREATE OR REPLACE PROCEDURE return_id_grade(
                                        subjectId IN NUMBER, 
                                        semesterYear IN DATE , 
                                        n IN INT, 
                                       -- options IN INT
                                        ,p_idgrade OUT ty_obj_idgrade) IS
       BEGIN
            SELECT DISTINCT student_id --INTO studentId,
                            ,grade --INTO studentGrade
                       INTO p_idgrade.studentId
                            ,p_idgrade.grade
                       FROM (SELECT studentId
                                    ,grade
                                    ,dense_rank() over (ORDER BY grade desc) rank 
                                    ,subject_id
                                    ,semester_year
                               FROM old_students )
                WHERE  subject_id = subjectId
                  AND  semester_year = semesterYear
                  AND  rank = n
                  AND  rownum <= 1;

EXCEPTION
WHEN OTHERS THEN
    dbms_output.put_line('we are inside when others -->'||sqlerrm);

END;

调用您的过程。

由于将选项用作IN参数,因此应在prc / fnc之外使用     所以可以在prc / fnc调用之后完成     如果选项= 1     然后        值:= p_idgrade.conatct     其他        值:= p_idgrade.grade     END IF;

希望有帮助。

答案 1 :(得分:0)

没有尝试编译它,但是类似这样的东西应该会关闭。

CREATE FUNCTION return_id_grade(subjectId IN NUMBER, semesterYear IN DATE , n IN INT, option IN INT)
RETURN NUMBER IS
studentId NUMBER(5),
studentGrade NUMBER(2,1);
   BEGIN
                SELECT DISTINCT student_id, grade
                INTO studentId, studentGrade
                    FROM (SELECT studentId, grade, dense_rank() over (ORDER BY grade desc) rank FROM old_students) 
                        WHERE subject_id = subjectId 
                            AND semester_year = semesterYear
                            AND rank = n
                            AND rownum <= 1;
    IF option = 1 then 
         RETURN studentId ;
    ELSE
        RETURN studentGrade ;
    END IF;
    END;

END;

但是,此功能确实不是一个好的设计。一个功能应该执行一个任务。如果要返回两个项目,请创建一个PL / SQL记录类型,并使用带有OUT参数的存储过程,然后在该过程中返回它。

答案 2 :(得分:0)

您可以直接在查询中尝试使用OPTION,如下所示:

CREATE FUNCTION RETURN_ID_GRADE (
    SUBJECTID      IN             NUMBER,
    SEMESTERYEAR   IN             DATE,
    N              IN             INT,
    OPTION         IN             INT
) RETURN NUMBER IS
    LV_RETURN_NUMBER   NUMBER(6, 1);
BEGIN
    -- QUERY TO FETCH REQUIRED DATA ONLY
    SELECT -- DISTINCT -- DISTINCT IS NOT NEEDED AS ROWNUM <= 1 IS USED
        CASE
            WHEN OPTION = 1 THEN STUDENT_ID
            ELSE GRADE
        END AS LV_RETURN_NUMBER
    INTO LV_RETURN_NUMBER -- STORE VALUE BASED ON OPTION
    FROM
        (
            SELECT
                STUDENTID,
                GRADE,
                DENSE_RANK() OVER(
                    ORDER BY
                        GRADE DESC
                ) RANK
            FROM
                OLD_STUDENTS
        )
    WHERE
        SUBJECT_ID = SUBJECTID
        AND SEMESTER_YEAR = SEMESTERYEAR
        AND RANK = N
        AND ROWNUM <= 1;

    RETURN LV_RETURN_NUMBER; -- RETURN THE VARIABLE

END;

干杯!