Oracle--函数

 开发者博客www.developsearch.com

 

 

简单函数:根据ID获取别名

 FUNCTION GET_SET_ALIAS_BY_SETID(IN_SETID IN VARCHAR2) RETURN VARCHAR2 AS
    V_SET_ALIAS VARCHAR2(60);
  BEGIN
    SELECT T.ALIAS
      INTO V_SET_ALIAS
      FROM CDH_NEWRBT_SET T
     WHERE T.SET_ID = IN_SETID;
    RETURN V_SET_ALIAS;
  END;

 

 

函数: 字符串分割

-- 创建组合SETIDS表集合类型
CREATE OR REPLACE TYPE TBL_SPLIT_TYPE IS TABLE OF VARCHAR2 (4000);
  
FUNCTION SPLIT_STR(  P_STR       IN VARCHAR2,
                     P_DELIMITER IN VARCHAR2 DEFAULT (','))
    RETURN TBL_SPLIT_TYPE IS
    J        INT := 0;
    I        INT := 1;
    LEN      INT := 0;
    LEN1     INT := 0;
    STR      VARCHAR2(4000);
    MY_SPLIT TBL_SPLIT_TYPE := TBL_SPLIT_TYPE();
  BEGIN
    LEN  := LENGTH(P_STR);
    LEN1 := LENGTH(P_DELIMITER);
  
    WHILE J < LEN LOOP
      J := INSTR(P_STR, P_DELIMITER, I);
    
      IF J = 0 THEN
        J   := LEN;
        STR := SUBSTR(P_STR, I);
        MY_SPLIT.EXTEND;
        MY_SPLIT(MY_SPLIT.COUNT) := STR;
      
        IF I >= LEN THEN
          EXIT;
        END IF;
      ELSE
        STR := SUBSTR(P_STR, I, J - I);
        I   := J + LEN1;
        MY_SPLIT.EXTEND;
        MY_SPLIT(MY_SPLIT.COUNT) := STR;
      END IF;
    END LOOP;
  
    RETURN MY_SPLIT;
  END;

 

检查维度是否存在交集

FUNCTION COUNT_DMNSN(IN_DMNSN_A IN NUMBER, IN_DMNSN_B IN NUMBER)
    RETURN NUMBER AS
    V_COUNT NUMBER;
  BEGIN
    -- 任意一方为-1(全选) 都表示存在交集 将V_COUNT手工置值为1
    IF (IN_DMNSN_A = -1 OR IN_DMNSN_B = -1) THEN
      V_COUNT := 1;

      -- 两者都不为-1的情况需要铺开判断是否存在交集
    ELSE
      SELECT COUNT(0)
        INTO V_COUNT
        FROM (SELECT D.CODE
                FROM CDH_NEWRBT_SET_DMNSN D
               WHERE D.DMNSN_ID = IN_DMNSN_A
              INTERSECT
              SELECT D.CODE
                FROM CDH_NEWRBT_SET_DMNSN D
               WHERE D.DMNSN_ID = IN_DMNSN_B);

    END IF;
    -- 返回结果
    RETURN V_COUNT;
  END;

 

 开发者博客www.developsearch.com

 

 

 

你可能感兴趣的:(oracle)