过程中的查询返回硬编码值和变量

时间:2017-04-26 21:53:55

标签: oracle plsql

我在pl / sql开发人员中有这个过程:

  procedure CURRICULUM_TRANSLATE2 (DEGRCODE IN OUT VARCHAR2,
                                 COLLCODE IN OUT VARCHAR2,
                                 MAJRCODE IN OUT VARCHAR2,
                                 RESDCODE IN OUT VARCHAR2,
                                 LEVLCODE IN OUT VARCHAR2,
                                 STYPCODE IN OUT VARCHAR2,
                                 TERMCODE IN OUT VARCHAR2,
                                 ATTSCODE IN OUT VARCHAR2,
                                 DEBUGGER IN     VARCHAR2 DEFAULT NULL)
IS
VARPROG       VARCHAR2(4000);
BEGIN
  SELECT
    T.DEGRCODE,
    T.PROGCODE,
    T.MAJRCODE,
    T.COLLCODE,                             
    T.LEVLCODE,
    T.STYPCODE,
    T.ATTSCODE
  INTO
   DEGRCODE,
   VARPROG ,
   MAJRCODE,
   COLLCODE,                             
   LEVLCODE,
   STYPCODE,
   ATTSCODE
  FROM 
    MAJORLOOKUP T
  JOIN (SELECT SRM_COLLEGE, SRM_DEGREE, SRM_MAJOR, MAX(SRM_TERM_EFFECTIVE) AS MAXTERM
        FROM   MAJORLOOKUP
        WHERE  SRM_TERM_EFFECTIVE <= TERMCODE
        GROUP BY SRM_COLLEGE, SRM_DEGREE, SRM_MAJOR) M  
    ON  M.SRM_COLLEGE = T.SRM_COLLEGE 
    AND M.SRM_DEGREE = T.SRM_DEGREE 
    AND ((T.SRM_MAJOR IS NULL AND M.SRM_MAJOR IS NULL)           OR 
         (T.SRM_MAJOR IS NULL AND M.SRM_MAJOR ='Not Applicable') OR 
         (T.SRM_MAJOR IS NULL AND UPPER(M.SRM_MAJOR) ='NONE')    OR 
         (T.SRM_MAJOR = M.SRM_MAJOR)) 
  WHERE T.SRM_TERM_EFFECTIVE = M.MAXTERM
  AND TRIM(M.SRM_COLLEGE) = TRIM(COLLCODE)
  AND TRIM(M.SRM_DEGREE) = TRIM(DEGRCODE)
  AND ((TRIM(M.SRM_MAJOR) IS NULL AND TRIM(MAJRCODE) IS NULL) OR (TRIM(M.SRM_MAJOR) = TRIM(MAJRCODE))) 
  ;

EXCEPTION
  WHEN OTHERS THEN
    IF DEBUGGER = 'Y' THEN
      ATTSCODE := SQLERRM;
    ELSE
      RETURN;  
    END IF;
END CURRICULUM_TRANSLATE2;

如果我测试此过程并输入数据,则全部指定为字符串。

collcode = School of Theology
degrcode = Master of Divinity
majrcode = General
termcode = 999999
debugger = Y

我没有结果。我可以说,因为我的ATTSCODE返回:

ORA-01403: no data found

但是,当我将查询修改为具有硬编码值时,如下所示:

  procedure CURRICULUM_TRANSLATE2 (DEGRCODE IN OUT VARCHAR2,
                                 COLLCODE IN OUT VARCHAR2,
                                 MAJRCODE IN OUT VARCHAR2,
                                 RESDCODE IN OUT VARCHAR2,
                                 LEVLCODE IN OUT VARCHAR2,
                                 STYPCODE IN OUT VARCHAR2,
                                 TERMCODE IN OUT VARCHAR2,
                                 ATTSCODE IN OUT VARCHAR2,
                                 DEBUGGER IN     VARCHAR2 DEFAULT NULL)
IS
VARPROG       VARCHAR2(4000);
BEGIN
  SELECT
    T.DEGRCODE,
    T.PROGCODE,
    T.MAJRCODE,
    T.COLLCODE,                             
    T.LEVLCODE,
    T.STYPCODE,
    T.ATTSCODE
  INTO
   DEGRCODE,
   VARPROG ,
   MAJRCODE,
   COLLCODE,                             
   LEVLCODE,
   STYPCODE,
   ATTSCODE
  FROM 
    MAJORLOOKUP T
  JOIN (SELECT SRM_COLLEGE, SRM_DEGREE, SRM_MAJOR, MAX(SRM_TERM_EFFECTIVE) AS MAXTERM
        FROM   MAJORLOOKUP
        WHERE  SRM_TERM_EFFECTIVE <= TERMCODE
        GROUP BY SRM_COLLEGE, SRM_DEGREE, SRM_MAJOR) M  
    ON  M.SRM_COLLEGE = T.SRM_COLLEGE 
    AND M.SRM_DEGREE = T.SRM_DEGREE 
    AND ((T.SRM_MAJOR IS NULL AND M.SRM_MAJOR IS NULL)           OR 
         (T.SRM_MAJOR IS NULL AND M.SRM_MAJOR ='Not Applicable') OR 
         (T.SRM_MAJOR IS NULL AND UPPER(M.SRM_MAJOR) ='NONE')    OR 
         (T.SRM_MAJOR = M.SRM_MAJOR)) 
  WHERE T.SRM_TERM_EFFECTIVE = M.MAXTERM
  AND TRIM(M.SRM_COLLEGE) = TRIM('School of Theology')
  AND TRIM(M.SRM_DEGREE) = TRIM('Master of Divinity')
  AND ((TRIM(M.SRM_MAJOR) IS NULL AND TRIM('General') IS NULL) OR (TRIM(M.SRM_MAJOR) = TRIM('General'))) 
  ;

EXCEPTION
  WHEN OTHERS THEN
    IF DEBUGGER = 'Y' THEN
      ATTSCODE := SQLERRM;
    ELSE
      RETURN;  
    END IF;
END CURRICULUM_TRANSLATE2;

我得到了我正在寻找的回报。

这是我正在使用的表的创建脚本:

-- Create table
create table MAJORLOOKUP
(
  srm_college        VARCHAR2(4000),
  srm_degree         VARCHAR2(4000),
  srm_major          VARCHAR2(4000),
  srm_term_effective NUMBER,
  degrcode           VARCHAR2(4000),
  progcode           VARCHAR2(4000),
  majrcode           VARCHAR2(4000),
  collcode           VARCHAR2(4000),
  levlcode           VARCHAR2(4000),
  stypcode           VARCHAR2(4000),
  attscode           VARCHAR2(4000)
)
tablespace USERS
  pctfree 10
  initrans 1
  maxtrans 225
  storage
  (
    initial 64K
    minextents 1
    maxextents unlimited
  );

我已经能够在两种环境中复制它。

我也使用相同的程序取得了成功,但在程序中有TERMCODE变量。但是,当我将硬编码文本交换出来并使用任何其他变量时,我得不到任何结果。

我已经尝试在桌面上重新设置自联接,但是我得到了同样的错误。

非常感谢。

============= EDITS =====

感谢您的回复。我已经将第一个代码块更新为具有变量的过程。我还更新了输入变量和输出代码。我为之前的疏忽道歉。

广义表名

0 个答案:

没有答案