Oacle function 작은 예

3521 단어 function
오늘 function 을 써 서 orcale 의 문법 을 배 웠 습 니 다. 다음은 나중에 볼 수 있 도록 수 정 된 간결 한 코드 입 니 다.
CREATE OR REPLACE TYPE typetmp AS TABLE OF VARCHAR2(4000);


CREATE OR REPLACE FUNCTION functiontmp(l_orders IN VARCHAR2)
RETURN typetmp
IS
  l_typetmp typetmp:= typetmp(); --   typetmp,           
  noLen INTEGER := 8;

  l_order VARCHAR2(3000);
  l_orderIds VARCHAR2(3000);     --input data
  l_strs VARCHAR2(3000);         --DNCContactNo Strings
  l_str VARCHAR2(3000);          --each DNCContactNo String
  l_orderId VARCHAR2(3000);      --each order
  
  ---------------------------declare cursor start-------------------------------
  CURSOR t_cursor IS
   SELECT coltmp  
        FROM tabletmp
        WHERE datetmp >= trunc(sysdate-1) and datetmp < trunc(sysdate); 
  ---------------------------declare cursor end---------------------------------
        
BEGIN

  ---------------------------get orderIDs start---------------------------------
    cusor           l_orderIds ,    fetch into
  ------------------------------------------------------------------------------
  IF l_orders IS NULL THEN
    BEGIN
     OPEN t_cursor; 
       FETCH t_cursor INTO l_order;
       
       WHILE t_cursor%FOUND LOOP--judge whether get last record
         IF l_orderIds IS NULL THEN
           l_orderIds := l_order||',';
         ELSE
           l_orderIds := l_order||','||l_orderIds||',';
         END IF;
         FETCH t_cursor INTO l_order;--loop cursor for get each record
       END LOOP;
       
      EXCEPTION
      WHEN OTHERS THEN
        CLOSE t_cursor;
      
      IF t_cursor%isopen THEN        
        CLOSE t_cursor;    
      END IF;
    END;
  END IF;
  
  --------------------------get orderIDs end------------------------------------
  
  IF l_orders<>'' OR l_orders IS NOT NULL THEN
    l_orderIds := l_orders||',';
  END IF;
  
  
  WHILE l_orderIds IS NOT NULL LOOP
    l_orderId := SUBSTR(l_orderIds, 1, INSTR(l_orderIds, ',')-1);
    --instr()      ',' l_orderIds    ,     


    ------------------------get orderid value start------------------------------
	  "select into from"        ,        ,       
	  table         l_strs,      ,    null
	------------------------------------------------------------------------
    IF l_orderId IS NOT NULL THEN
      BEGIN
        SELECT b.coltmp1 INTO l_strs FROM tabletmp b where col=l_orderId;
      EXCEPTION
      WHEN   no_data_found   THEN
        l_strs := NULL;
      END;
    END IF;
    ------------------------get orderid value end------------------------------


    WHILE l_strs IS NOT NULL LOOP
      l_str := substr(l_strs, 1, noLen);

      l_typetmp.EXTEND(1);
      l_typetmp(l_typetmp.COUNT) := l_str;--         l_typetmp    

      l_strs := SUBSTR(l_strs, noLen+2);
      l_str := NULL;
    END LOOP;
	
      l_strs := NULL;

      l_orderIds := SUBSTR(l_orderIds, length(l_orderId)+2);

  END LOOP;

  RETURN l_typetmp;
END;





좋은 웹페이지 즐겨찾기