结果集变量或查询的PL / SQL返回类型不匹配

时间:2014-10-03 21:42:17

标签: oracle plsql

我正在尝试使用GET存储过程/光标来显示我在彩票游戏数据库中创建的彩票游戏桌的游戏名称。

以下是代码:

CREATE OR REPLACE PROCEDURE GetLotteryGameName (
  p_lgid IN NUMBER,
  p_value out VARCHAR2,
  p_field IN VARCHAR2
)
IS
BEGIN
  SELECT GAMENAME
  INTO p_value
  FROM LOTTERYGAME
  WHERE LOTTERYGAMEID = p_lgid;
END GetLotteryGameName;

CREATE OR REPLACE PACKAGE GETLOTTERYGAMENAMEPKG IS
  PROCEDURE GetLotteryGameName (
    p_lgid IN NUMBER,
    p_value out VARCHAR2,
    p_field IN VARCHAR2
  );

  TYPE per_ref_cursor IS REF CURSOR;
  PROCEDURE GetGameName (p_lgid IN NUMBER, p_ref OUT per_ref_cursor);

END GETLOTTERYGAMENAMEPKG;

CREATE OR REPLACE PACKAGE BODY GETLOTTERYGAMENAMEPKG IS
  PROCEDURE GetLotteryGameName (
    p_lgid IN NUMBER,
    p_value out VARCHAR2,
    p_field IN VARCHAR2
  )
  IS
  BEGIN
    SELECT GAMENAME
    INTO p_value
    FROM LOTTERYGAME
    WHERE LOTTERYGAMEID = p_lgid;
  END GetLotteryGameName;

  PROCEDURE GetName
    (p_lgid IN NUMBER,
     p_ref OUT per_ref_cursor) IS
     BEGIN
      OPEN p_ref FOR
        SELECT GAMENAME
        FROM LOTTERYGAME
        WHERE LOTTERYGAMEID = p_lgid;
    END GetName;

END GETLOTTERYGAMENAMEPKG;

DECLARE
  v_cursor    GETLOTTERYGAMENAMEPKG.per_ref_cursor;
  v_lgid      LOTTERYGAME.LOTTERYGAMEID%TYPE;
  v_gamename  LOTTERYGAME.GAMENAME%TYPE;

BEGIN
  GETLOTTERYGAMENAMEPKG.GetName (p_lgid = 2,
                                 p_ref => v_cursor);
  LOOP
    FETCH v_cursor
    INTO v_lgid, v_gamename;
    EXIT WHEN v_cursor%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE(v_lgid || ',' || v_gamename);
  END LOOP;
  CLOSE v_cursor;
END;

当我运行上面的声明语句时,我收到以下错误:

06504. 00000 -  "PL/SQL: Return types of Result Set variables or query do not match"
*Cause:    Number and/or types of columns in a query does not match declared
           return type of a result set  variable, or declared types of two Result
           Set variables do not match.

我知道在这段代码中某处出现了错误,我只是不知道我在寻找什么,或者如何设置自己以避免将来失败。

非常感谢任何帮助,

干杯,

FBF

2 个答案:

答案 0 :(得分:5)

如错误所示,您SELECT中的GAMENAMESELECT,但会尝试输入v_lgid, v_gamename

所以将LOTTERYGAMEID添加到CURSOR's SELECT 中 或者更改您的匿名阻止。

  PROCEDURE GetName
    (p_lgid IN NUMBER,
     p_ref OUT per_ref_cursor) IS
     BEGIN
      OPEN p_ref FOR
        SELECT GAMENAME,LOTTERYGAMEID
        FROM LOTTERYGAME
        WHERE LOTTERYGAMEID = p_lgid;
    END GetName;

答案 1 :(得分:0)

您还应该更正语法错误:

GETLOTTERYGAMENAMEPKG.GetName (p_lgid = 2,
                               p_ref => v_cursor);

应阅读

GETLOTTERYGAMENAMEPKG.GetName (p_lgid => 2,
                               p_ref => v_cursor);

分享并享受。