顯示具有 PLSQL 標籤的文章。 顯示所有文章
顯示具有 PLSQL 標籤的文章。 顯示所有文章

2012/5/16

[PLSQL] FUNCTION 使用ROWTYPE 回傳值範例


PLSQL – 使用ROWTYPE 回傳值
Author: Willie
Date: 2012/05/16

目的:Function Return Row, 可以方便使用Function來更新欄位


範例程式
// Package 的表頭
create or replace package WT_CST_TESET is
   
   //宣告要回傳的Row裡面有哪些欄位  
   cursor cr_po_line is
            select l.po_line_id,l.po_header_id,l.item_id, l.unit_price
            from po_lines_all l;

      procedure main;
      function get_lines( po_line_id number) return cr_po_line%rowtype;           
end WT_CST_TESET;
/
//Package 的表身
create or replace package body WT_CST_TESET is

       procedure main is
            
            r_line cr_po_line%rowtype;
            cursor c1 is
                  select l.po_line_id,l.po_header_id,l.item_id, l.unit_price
                  from po_lines_all l
                  where PO_LINE_ID >=3205 AND PO_LINE_ID<=4000 ;
       begin
                     
      
            for r1 in c1 loop
               
                //取得Row的回傳                    
                r_line:=get_lines(r1.po_line_id);
               
                update po_lines_all pal
                set pal.po_line_id = r_line.po_line_id,
                    pal.item_id = r_line.item_id
                where pal.po_line_id = r1.po_line_id;
               
            end loop;
                
       end main;

      //回傳整筆Row的Function,利用 cr_po_line 的row type做回傳

       function get_lines( po_line_id number) return cr_po_line%rowtype
       is   
           return_row cr_po_line%rowtype;
       begin
          select l.po_line_id,l.po_header_id,l.item_id, l.unit_price
          into return_row
          from po_lines_all l
          where l.po_line_id = po_line_id;
         
          return return_row;
       end get_lines;
      
end WT_CST_TESET;
/


2012/5/7

[PL/SQL] PLSQL Split Chinese Words 中文字長度切割


PL/SQL中文字長度切割
Author: Raymond
Date:2012/05/07

中文字在 PLSQL DB 裡佔了3個char的位子,利用這個特性我們算出正確欲結束的字元位址並製成一個Function供大家參考




/*********************************************************
      2012.03 Raymond / Willie Chou
      Input Parameter - 1: 輸入字串,  2. 長度限制
      Output  parameter: 切字串的位置 substr(input_string, 1, output_parameter);
***********************************************************/
    
function split_loc(
          input_str varchar2-- input string,
          limit_byte  number-- length limitation (in bytes)
        return number          -- split location
        is 
          loc number(5);                                                
          max_len number(5);
         
        begin          
          loc := trunc(length(limit_byte) / 3);
          max_len := length(input_str);
          loop
             exit when lengthb(substr(input_str,1,loc+1)) > limit_byte or (loc + 1) > max_len;
            loc := loc + 1;
          end loop;
          return loc;
     end;