程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SyBase數據庫 >> SyBase教程 >> 一個sybase函數(1)

一個sybase函數(1)

編輯:SyBase教程

create or replace function fn_hw_PreStatGetTableIdx
(
    in p_sTblName  varchar(60),
    in p_BeginTime varchar(20),
    in p_EndTime   varchar(20)
)
returns varchar(4000)
begin
    declare v_iFlag      int;
    declare v_iType      int;
    declare v_icount     int;
    declare v_iStarttime int;
    declare v_iEndTime   int;
    declare v_iCurrent   int;
    declare v_iTmpTime   int;
    declare v_sTblSuff   varchar(4000);
   
    set v_sTblSuff = 0;
   
    --判斷傳入的表名稱是否在配置表中配置
    select count(*) into v_icount from cfg_sub_table_para where sTableName = p_sTblName;
   
    --從配置表獲取分表方式
    if ( v_icount <> 0 ) then
        select iFlag,iType into v_iFlag,v_iType from cfg_sub_table_para
            where sTableName = p_sTblName;
    else
        --如果沒有配置默認為不分表
        set v_sTblSuff = 0;
        return v_sTblSuff;
    end if;
   
    --當不分表的時候,直接返回0,如果分表,將起始結束時間范圍內的表後綴返回
    if ( v_iFlag = 0 ) then
        set v_sTblSuff = 0;
    else
        case v_iType
        when 0 then    --按天分表
            if(p_BeginTime = ) then
                set v_iCurrent = datediff(day,1970-01-01 00:00:00,getdate() + 1);
                set v_sTblSuff = cast(v_iCurrent as varchar(128));
                return v_sTblSuff;
            end if;
           
            set v_iStarttime = datediff(day,1970-01-01 00:00:00,convert(datetime,p_BeginTime,20));
            set v_iEndtime = datediff(day,1970-01-01 00:00:00,convert(datetime,p_EndTime,20));
           
            --循環將按天分表的表後綴拼接成字符串,以","分隔。
            if ( v_iStarttime = v_iEndtime ) then
                set v_sTblSuff = cast(v_iStarttime as varchar(128));
            else
                set v_iTmpTime = v_iStarttime;
                --拼接返回值
                while ( v_iTmpTime <> (v_iEndtime + 1) ) loop
                    if ( v_sTblSuff = 0 ) then
                        set v_sTblSuff = cast(v_iTmpTime as varchar(128));
                    else
                        set v_sTblSuff = v_sTblSuff||,||cast(v_iTmpTime as varchar(128));
                    end if;
                   
                    set v_iTmpTime = v_iTmpTime + 1;
                end loop;
            end if;
        else
            --不在取值范圍內默認為不分表
            set v_sTblSuff = 0;
        end case;
    end if;
   
    --返回結果
    return v_sTblSuff;
end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved