#********************************************************************
# 功能描述:定義公共的函數
# 依賴關係:依賴於全局aitools工具包(由於存在對數據庫的操做)
# 調用方法:無
# 數據流向:無
# 建立人:wengdan
# 建立日期:20151016
# 修改歷史:
# 序號 日期 修改人 修改緣由 修改內容
#*********************************************************************sql
# include.tcl定義了一些公共變量
#source /crm/crmwk/aiomnivision/bin/include.tcl數據庫
####################################################################################################################################################################
#程序執行函數
#################函數
#*********************************************************************
# 功能描述:執行sql,執行後將返回成功失敗標誌,並提交數據庫
# 數據流向:無
# 參數說明:
# sqlStr: 表示將執行的sql
# errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql
#
# 返回值說明:
# -1 執行失敗
# 0 執行成功
#*********************************************************************
proc exeSql {p_sqlStr p_errCode} {
set rt [ _exeSql $p_sqlStr $p_errCode 1 1 ]
return $rt
}工具
#*********************************************************************
# 功能描述:執行sql(原型)
# 數據流向:無
# 參數說明:
# sqlStr: 表示將執行的sql
# errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql
# isReturn: 表示sql執行是否判斷aidb_sql的執行結果並返回(若是返回-1將致使程序結束運行),默認返回
# 1:表示返回錯誤 0:表示不返回錯誤
# isCommit: 表示sql執行後是否提交
# 1:表示執行完後當即提交 0:表示不當即提交
#
# 返回值說明:
# -1 執行失敗
# 0 執行成功
#*********************************************************************
proc _exeSql {p_sqlStr p_errCode p_isReturn p_isCommit} {
global conn
global handlefetch
traceLog "======= 開始執行SQL語句 =======" ""
traceLog ${p_sqlStr} ""
if [catch { aidb_sql ${handle} ${p_sqlStr} } errmsg ] {日誌
traceLog [string range ${errmsg} 30 [string length ${errmsg}] ] ${p_errCode}
set systime [exec date "+%Y-%m-%d %H:%M:%S"]
puts "\[${systime}\] -> ${p_sqlStr} "
puts "\[${systime}\] -> ERROR:[string range ${errmsg} 30 [string length ${errmsg}] ] LINE=${p_errCode}"code
if { ${p_isReturn} == 1 } {
return -1
}
}
if { ${p_isCommit} == 1 } {
aidb_commit ${conn}
}
traceLog "======= SQL語句執行結束 =======" ""
return 0
}orm
#*********************************************************************
# 功能描述:檢查某表是否在數據庫中存在
# 數據流向:無
# 參數說明:檢查的表名
# 返回值說明:
# 0 不存在
# 1 存在
# -1 檢查出錯
#*********************************************************************
proc isTabExist {p_tabName} {
global conn
global handleip
set tabNm [string toupper $p_tabName]
# 檢查syscat.tables系統表
# 20100421,wanggy@asiainfo.com增長對數據庫當前SCHEMA的判斷
set sqlstat "select count(1) from syscat.tables where tabname='$tabNm' and tabschema = current schema with ur"
if [catch {aidb_sql $handle $sqlstat} errmsg] {
trace_sql $errmsg 1000
# 若是語句執行出錯,返回-1
return -1
} else {
set v [aidb_fetch $handle ]
aidb_commit $conn
aidb_close $handle
set handle [aidb_open $conn]
if {$v != 1} {
return 0
} else {
return 1
}
}
}
####################################################################################################################################################################
#日期函數字符串
#*********************************************************************
# 功能描述:1.一、獲取參很多天期yyyymmdd格式
# 參數說明:yyyy-mm-dd
# 返回值說明: 參數月[日期],格式:yyyymmdd
#*********************************************************************
proc get_yyyymmdd {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
return ${year}${month}${day}
}
#*********************************************************************
# 功能描述:1.二、獲取參很多天期yyyymm格式
# 參數說明:yyyy-mm-dd
# 返回值說明: 參數月[日期],格式:yyyymm
#*********************************************************************
proc get_yyyymm {p_optime} {
set year 0
set month 0
scan $p_optime "%04s-%02s" year month
return ${year}${month}
}
#*********************************************************************
# 功能描述:1.三、獲取參很多天期yyyy-mm格式
# 參數說明:yyyy-mm-dd
# 返回值說明: 參數月[日期],格式:yyyy-mm
#*********************************************************************
proc get_l_yyyymm {p_optime} {
set year 0
set month 0
scan $p_optime "%04s-%02s" year month
return ${year}-${month}
}
#*********************************************************************
# 功能描述:1.四、獲取參很多天期的年
# 參數說明:yyyy-mm-dd
# 返回值說明: 參數月[日期]所在年,格式:yyyy
#*********************************************************************
proc get_yyyy {p_optime} {
set year 0
scan $p_optime "%04s" year
return ${year}
}
#*********************************************************************
# 功能描述:1.五、獲取某日所在月的總天數
# 數據流向:無
# 參數說明:yyyy-mm[-dd]
# 返回值說明: 參數月[日期]所在月的總天數,格式:dd
#*********************************************************************
proc get_days {p_optime} {
set year 0
set month 0
scan $p_optime "%04s-%02s" year month
set cal [ exec cal $month $year ]
set lastMday [lindex $cal end]
return ${lastMday}
}
#*********************************************************************
# 功能描述:1.六、獲取當月最後一天的日期
# 參數說明:yyyy-mm[-dd]
# 返回值說明:參數月[日期]所在月的最後一天的日期,格式yyyy-mm-dd
#*********************************************************************
proc get_l_ymds {p_optime} {
set year 0
set month 0
scan $p_optime "%04s-%02s" year month
set lastMday [ get_days $p_optime ]
return ${year}-${month}-${lastMday}
}
#*********************************************************************
# 功能描述:1.七、獲取當月最後一天的日期
# 參數說明:yyyy-mm[-dd]
# 返回值說明:參數月[日期]所在月的最後一天的日期,格式yyyymmdd
#*********************************************************************
proc get_ymds {p_optime} {
set year 0
set month 0
scan $p_optime "%04s-%02s" year month
set lastMday [ get_days $p_optime ]
return ${year}${month}${lastMday}
}
#*********************************************************************
# 功能描述:2.一、獲取某日的前一天日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前一天日期,格式爲yyyy-mm-dd
#*********************************************************************
proc get_l_pyyyymmdd {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04d-%02d-%02d" year month day
if { $day == 1 && $month == 1} {
# 返回上年12月31日
set day 31
set month 12
set year [expr $year -1]
} elseif {$day == 1} {
# 返回前月最後一天
set month [expr $month - 1]
set cal [exec cal $month $year]
set day [lindex $cal end]
} else {
# 返回同年同月前一天
set day [expr $day - 1]
}
set month [format "%02s" $month]
set day [format "%02s" $day]
return $year-$month-$day
}
#*********************************************************************
# 功能描述:2.二、獲取某日的前一天日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前一天日期,格式爲yyyymmdd
#*********************************************************************
proc get_pyyyymmdd {p_optime} {
set year 0
set month 0
set day 0
set tmp_optime [ get_l_pyyyymmdd $p_optime ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}${month}${day}
}
#*********************************************************************
# 功能描述:2.三、獲取某日的前一天的年月
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前一天的年月,格式爲yyyy-mm
#*********************************************************************
proc get_l_pyyyymm {p_optime} {
set year 0
set month 0
set tmp_optime [ get_l_pyyyymmdd $p_optime ]
scan $tmp_optime "%04s-%02s" year month
return ${year}-${month}
}
#*********************************************************************
# 功能描述:2.四、獲取某日的前一天的年月
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前一天的年月,格式爲yyyymm
#*********************************************************************
proc get_pyyyymm {p_optime} {
set year 0
set month 0
set tmp_optime [ get_l_pyyyymmdd $p_optime ]
scan $tmp_optime "%04s-%02s" year month
return ${year}${month}
}
#*********************************************************************
# 功能描述:2.五、獲取某日的前一天的年
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前一天的年,格式爲yyyy
#*********************************************************************
proc get_pyyyy {p_optime} {
set year 0
set tmp_optime [ get_l_pyyyymmdd $p_optime ]
scan $tmp_optime "%04s" year
return ${year}
}
#*********************************************************************
# 功能描述:3.一、獲取前月最後一日 日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前月最後一天的日期,格式爲yyyy-mm-dd
#*********************************************************************
proc get_l_lymds {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
set tmp_optime [ get_l_pyyyymmdd ${year}-${month}-01 ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}-${month}-${day}
}
#*********************************************************************
# 功能描述:3.二、獲取前月最後一日 日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前月最後一天的日期,格式爲yyyymmdd
#*********************************************************************
proc get_lymds {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
set tmp_optime [ get_l_pyyyymmdd ${year}-${month}-01 ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}${month}${day}
}
#*********************************************************************
# 功能描述:3.三、獲取前月最後一日 日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前月最後一天的日期,格式爲yyyy-mm
#*********************************************************************
proc get_l_lyyyymm {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
set tmp_optime [ get_l_pyyyymmdd ${year}-${month}-01 ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}-${month}
}
#*********************************************************************
# 功能描述:3.四、獲取前月最後一日 日期
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前月最後一天的日期,格式爲yyyymm
#*********************************************************************
proc get_lyyyymm {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
set tmp_optime [ get_l_pyyyymmdd ${year}-${month}-01 ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}${month}
}
#*********************************************************************
# 功能描述:3.五、獲取前月最後一日 年
# 參數說明:yyyy-mm-dd
# 返回值說明: 基準日期的前月最後一天的日期,格式爲yyyy
#*********************************************************************
proc get_lyyyy {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04s-%02s-%02s" year month day
set tmp_optime [ get_l_pyyyymmdd ${year}-${month}-01 ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}
}
#*********************************************************************
# 功能描述:3.六、上月同期
# 參數說明:yyyy-mm-dd
# 返回值說明: 參很多天期的上月同期,格式爲yyyy-mm-dd
#*********************************************************************
proc get_l_lmymd {p_optime} {
set year 0
set month 0
set day 0
scan $p_optime "%04d-%02d-%02d" year month day
set lastmday [ get_days $p_optime ]
#如果1月
if { $month == 1} {
# 返回上年12月
set month 12
set year [expr $year -1]
#如果3月末
} elseif {$month == 3 && $day >=29 } {
set month [expr $month -1]
#返回上月最後一天
set day [ get_days ${year}-${month}-01 ]
#如果月末
} elseif {$day == $lastmday} {
set month [expr $month -1]
#返回上月最後一天
set day [ get_days ${year}-${month}-01 ]
} else {
set month [expr $month -1]
}
set month [format "%02s" $month]
set day [format "%02s" $day]
return ${year}-${month}-${day}
}
#*********************************************************************
# 功能描述:3.七、上月同期
# 參數說明:yyyy-mm-dd
# 返回值說明: 參很多天期的上月同期,格式爲yyyymmdd
#*********************************************************************
proc get_lmymd {p_optime} {
set year 0
set month 0
set day 0
set tmp_optime [ get_l_lmymd ${p_optime} ]
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}${month}${day}
}
#*********************************************************************
# 功能描述:獲取某日的前若干天日期
# 數據流向:無
# 參數說明:
# 1 基準日期p_optime,格式yyyy-mm-dd
# 2 相隔天數p_delta,格式數字,無前導0
# 返回值說明:
# 基準日期的前p_delta天日期,格式爲yyyy-mm-dd
#*********************************************************************
proc get_l_Npyyyymmdd {p_optime p_delta} {
set year 0
set month 0
set day 0
set tmp_optime $p_optime
for { set i 1 } { $i <= $p_delta } { incr i } {
set tmp_optime [ get_l_pyyyymmdd $tmp_optime ]
}
return $tmp_optime
}
#*********************************************************************
# 功能描述:獲取某日的前若干天日期
# 數據流向:無
# 參數說明:
# 1 基準日期p_optime,格式yyyy-mm-dd
# 2 相隔天數p_delta,格式數字,無前導0
# 返回值說明:
# 基準日期的前p_delta天日期,格式爲yyyymmdd
#*********************************************************************
proc get_Npyyyymmdd {p_optime p_delta} {
set year 0
set month 0
set day 0
set tmp_optime $p_optime
for { set i 1 } { $i <= $p_delta } { incr i } {
set tmp_optime [ get_l_pyyyymmdd $tmp_optime ]
}
scan $tmp_optime "%04s-%02s-%02s" year month day
return ${year}${month}${day}
}
####################################################################################################################################################################
##*********************************************************************## 功能描述:日期格式轉化## 數據流向:無## 參數說明:## 1 p_optime 要轉化的日期,月、日數字必須帶前導0## 2 p_fmt_src 原始日期格式(yyyy-mm-dd,yyyymmdd)## 3 p_fmt_obj 目標日期格式(yyyy,-,mm,dd4種元素的組合,如yyyy-mm-dd,yyyy-mm,yyyy,mm,dd,yyyymmdd,yyyymm,mmdd)## 返回值說明:## 按p_fmt_obj的格式返回p_optime##*********************************************************************#proc formatDate { p_optime p_fmt_src p_fmt_obj } {# set year 0# set month 0# set day 0## # 根據原格式析取# if { $p_fmt_src == "yyyy-mm-dd" } {# scan $p_optime "%04s-%02s-%02s" year month day# } elseif { $p_fmt_src == "yyyymmdd" } {# scan $p_optime "%04s%02s%02s" year month day# } else {# traceLog "function formatDate() 不支持源日期格式描述${p_fmt_src}" "0501"# return ""# }## # 若是month、day爲空,則默認01# if {$day == "" || $day == 0 || $day == 00 } { set day [ format "%02d" 1 ] }# if {$month == "" || $month == 0 || $month == 00} { set month [ format "%02d" 1 ] }## # 根據結果格式轉化# if { $p_fmt_obj == "yyyymmdd" } {# set v ${year}${month}${day}# } elseif { $p_fmt_obj == "yyyymm" } {# set v ${year}${month}# } elseif { $p_fmt_obj == "yyyy-mm-dd" } {# set v ${year}-${month}-${day}# } elseif { $p_fmt_obj == "yyyy-mm" } {# set v ${year}-${month}# } elseif { $p_fmt_obj == "yyyy" } {# set v ${year}# } elseif { $p_fmt_obj == "mm" } {# set v ${month}# } elseif { $p_fmt_obj == "dd" } {# set v ${day}# } elseif { $p_fmt_obj == "mm-dd" } {# set v ${month}-${day}# } elseif { $p_fmt_obj == "mmdd" } {# set v ${month}${day}# } else {# traceLog "function formatDate() 不支持目標日期格式描述${p_fmt_src}" "0501"# return ""# }## # 返回結果# return ${v}#}#####*********************************************************************## 功能描述:獲取某日的前若干天日期## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 2 相隔天數p_delta,格式數字,無前導0## 返回值說明:## 基準日期的前p_delta天日期,格式爲yyyy-mm-dd##*********************************************************************#proc getPreDays {p_optime p_delta} {# set year 0# set month 0# set day 0## set tmp_optime $p_optime# for { set i 1 } { $i <= $p_delta } { incr i } {# set tmp_optime [ getPreDay $tmp_optime ]# }# return $tmp_optime#}###*********************************************************************## 功能描述:獲取某日的後一天日期## 數據流向:無## 參數說明:yyyy-mm-dd## 返回值說明:## 基準日期的後一天日期,格式爲yyyy-mm-dd##*********************************************************************#proc getNextDay {p_optime} {# set year 0# set month 0# set day 0# scan $p_optime "%04d-%02d-%02d" year month day# set lastMday [ getMonthDays $p_optime ]# if { $day == 31 && $month == 12} {# # 返回下年1月1日# set day 01# set month 01# set year [expr $year + 1]# } elseif {$day == $lastMday} {# # 返回下月第一天# set month [expr $month + 1]# set day 01# } else {# # 返回同年同月後一天# set day [expr $day + 1]# }# set month [format "%02d" $month]# set day [format "%02d" $day]## return $year-$month-$day#}###*********************************************************************## 功能描述:獲取某日的後若干天日期## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 2 相隔天數p_delta,格式數字,無前導0## 返回值說明:## 基準日期的後p_delta天日期,格式爲yyyy-mm-dd##*********************************************************************#proc getNextDays {p_optime p_delta} {# set year 0# set month 0# set day 0## set tmp_optime $p_optime# for {set i $1 } { $i <= $p_delta } { incr i } {# set tmp_optime [ getNextDay $tmp_optime ]# }# return $tmp_optime#}###*********************************************************************## 功能描述:獲取前月最後一天日期## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 返回值說明:## 基準日期的前月最後一天的日期,格式爲yyyy-mm-dd##*********************************************************************#proc getLastPMday {p_optime} {# set year 0# set month 0# set day 0## scan $p_optime "%04s-%02s-%02s" year month day# set tmp_optime [ getPreDay ${year}-${month}-01 ]# return $tmp_optime#}##*********************************************************************## 功能描述:獲取後月一號日期## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 返回值說明:## 基準日期的後月第一天的日期,格式爲yyyy-mm-dd##*********************************************************************#proc getFirstNextMday {p_optime} {# set lastMday [ getLastMday $p_optime ]# set firstNextMday [ getNextDay $lastMday ]# return $firstNextMday#}####*********************************************************************## 功能描述:獲取某日的星期數## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 返回值說明:## 基準日期的星期數:1-7##*********************************************************************#proc getWeekDay {p_optime} {# set year 0# set month 0# set day 0# scan $p_optime "%04d-%2d-%2d" year month day# set wd [ exec cal $month $year | awk "\{for(i=1;i<=NF;i++)\{if(\$i==$day && NR>3)\{print i\}else if(\$i==$day)\{print 7-NF+i \}\}\}" ]# set wd [ expr (${wd}-1+7) % 7 ]# if {$wd == 0} {# set wd 7# }# return $wd#}###*********************************************************************## 功能描述:獲取前日的星期數## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 返回值說明:## 基準日期前一日的星期數:1-7##*********************************************************************#proc getPreWeekDay {p_optime} {# set pre_day [ getPreDay $p_optime ]# set wd [ getWeekDay $pre_day ]# return $wd#}###*********************************************************************## 功能描述:根據邏輯表空間名稱獲取實際表空間名稱## 數據流向:無## 參數說明:邏輯表空間名稱## 返回值說明:## 實際表空間名稱##*********************************************************************#proc getTbsName {p_tbsName} {# return ${g::TBSMAP($p_tbsName)}#}###*********************************************************************## 功能描述:根據用戶名密碼配置獲取相應類型的用戶名和密碼## 數據流向:無## 參數說明:類型名,如## 返回值說明:## 用戶名## 備註:略,tcl程序不須要,在tcl調度程序中包含了統一的用戶名密碼獲取方法##*********************************************************************#proc getUserPass {p_type} {##}###*********************************************************************## 功能描述:根據用戶名密碼配置獲取相應類型的密碼## 數據流向:無## 參數說明:類型名## 返回值說明:## 密碼## 備註:略,tcl程序不須要,在tcl調度程序中包含了統一的用戶名密碼獲取方法##*********************************************************************#proc getPassword {p_type } {##}#####*********************************************************************## 功能描述:檢查某表是否在數據庫中存在## 數據流向:無## 參數說明:檢查的表名## 返回值說明:## 0 不存在## 1 存在## -1 檢查出錯##*********************************************************************#proc isTabExist2 {p_schema p_tabName} {# global conn# global handle## set tabNm [string toupper $p_tabName]# set schema [string toupper $p_schema]# # 檢查syscat.tables系統表# set sqlstat "select count(1) from syscat.tables where tabname='$tabNm' and tabschema = '$schema' with ur"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 1000# # 若是語句執行出錯,返回-1# return -1# } else {# set v [aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# if {$v != 1} {# return 0# } else {# return 1# }# }#}###*********************************************************************## 功能描述:寫日誌函數到對應tcl文件的.trace文件中## 數據流向:無## 參數說明:## p_loginfo 日誌內容,要打印到.trace文件的內容## p_errcode 日誌錯誤序號;若是是錯誤日誌,將根據本錯誤序號找到對應的程序TCL位置;## 傳空表示只是打印信息。錯誤序號1000之內爲保留號,供公共代碼使用。## 返回值說明:## 無##*********************************************************************#proc traceLog {p_logInfo p_errCode} {# global tracefd# global script## if { $p_errCode == "" } {# puts ${tracefd} "\[[clock format [clock seconds]]\] -> ${p_logInfo} \n"# } else {# puts ${tracefd} "\[[clock format [clock seconds]]\] -> ERROR:${p_logInfo} LINE=${p_errCode} \n"# }# flush ${tracefd}#}######*********************************************************************## 功能描述:功能同_exeSql,執行成功則提交數據庫,執行失敗則返回出錯信息## 數據流向:無## 參數說明:## sqlStr: 表示將執行的sql## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## errmsg 執行失敗## 0 執行成功## 建立人: shiquanzhong 20090319##*********************************************************************#proc _exeSql2 {p_sqlStr p_errCode p_isReturn p_isCommit} {# global conn# global handle## traceLog "======= 開始執行SQL語句 =======" ""# traceLog ${p_sqlStr} ""# if [catch { aidb_sql ${handle} ${p_sqlStr} } errmsg ] {# traceLog [string range ${errmsg} 30 [string length ${errmsg}] ] ${p_errCode}# if { ${p_isReturn} == 1 } {# return $errmsg# }# }# if { ${p_isCommit} == 1 } {# aidb_commit ${conn}# }# traceLog "======= SQL語句執行結束 =======" ""# return 0#}###*********************************************************************## 功能描述:功能同exeSql,執行成功則提交數據庫,失敗則返回出錯信息## 數據流向:無## 參數說明:## sqlStr: 表示將執行的sql## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## errmsg 執行失敗## 0 執行成功## 建立人: shiquanzhong 20090319##*********************************************************************#proc exeSql2 {p_sqlStr p_errCode} {# set rt [ _exeSql2 $p_sqlStr $p_errCode 1 1 ]# if {${rt} == 0 } {# return 0# } else {# return $rt# }#}####*********************************************************************## 功能描述:獲取某表記錄條數## 數據流向:無## 參數說明:獲取條數的表名## 返回值說明:## n 條數## -1 獲取時出錯##*********************************************************************#proc getTabRecNum {p_tabName} {# global conn# global handle## # 檢查表# set sqlstat "select count(1) from $p_tabName"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 不然返回條數# return $v# }#}###*********************************************************************## 功能描述:把字符串轉換爲數據(爲了解決08->8這種狀況,08在TCL中不識別)## 數據流向:無## 參數說明:字符串(數字字符串)## 返回值說明:## n 條數## -1 獲取時出錯##*********************************************************************#proc getNum {p_string} {# global conn# global handle## # 檢查表# set sqlstat "select int($p_string) from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 不然返回條數# return $v# }#}###*********************************************************************## 功能描述:上月同期## 數據流向:無## 參數說明:字符串(數字字符串)## 返回值說明:## n 條數## -1 獲取時出錯##*********************************************************************#proc getLastMonPeriod {p_string} {# global conn# global handle## # 檢查表# set sqlstat "select date('$p_string') - 1 month from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set vir_v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return ${vir_v}# }#}####****************************************************## 更新sp商code## 返回:-1 更新失敗 0 更新成功##****************************************************#proc updSpCode {} {# global conn# global handle## set tabNm [string toupper dim_ismg_svccode2]# set ret [isTabExist $tabNm]# if {$ret == 0} {# set sqlstat "# CREATE TABLE DIM_ISMG_SVCCODE2 (# SVCCODE_ID CHAR(15) NOT NULL ,# BEGIN_TIME DATE NOT NULL ,# END_TIME DATE NOT NULL ,# ACTIVE_FLAG SMALLINT ,# SVCCODE_NAME CHAR(64) NOT NULL ,# SP_DESC CHAR(6) NOT NULL ,# DESC_TXT VARCHAR(64) ,# SP_TYPE CHAR(1) )# PARTITIONING KEY (SP_DESC) USING HASHING# IN TBS_DIM# "# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 1000# return -1# } else {# aidb_commit $conn# }# }# # 首先清空sp商維表# set sqlstat "delete from DIM_ISMG_SVCCODE2"# puts $sqlstat# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 1000# return -1# } else {# aidb_commit $conn# }## # 而後插入st_nbmont_swmk_dm中的sp商代碼# #2005-11-21修改,將短信SP代碼sp_type=1,其它SP代碼寫爲2# set sqlstat "# insert into DIM_ISMG_SVCCODE2# select distinct '0','2004-01-01','2004-01-01',1,'',char(sp_code),'','1'# from st_nbmont_swmk_dm where busi_type=1# "# puts $sqlstat# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 1000# return -1# } else {# aidb_commit $conn# }# set sqlstat "# insert into DIM_ISMG_SVCCODE2# select distinct '0','2004-01-01','2004-01-01',1,'',char(sp_code),'','2'# from st_nbmont_swmk_dm where busi_type<>1# "# puts $sqlstat# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 1000# return -1# } else {# aidb_commit $conn# }# return 0#}###*********************************************************************## 功能描述:runstats一個表## 數據流向:無## 參數說明:## p_tabNm 表名(格式:schema_name.table_name)## 返回值說明:##*********************************************************************#proc runstatsTab {p_tabNm} {# traceLog "======= 開始執行runstats $p_tabNm ======" ""# exec db2 connect to ngcqdw# traceLog "db2 runstats on table $p_tabNm and index all" ""# exec db2 runstats on table $p_tabNm and index all# exec db2 terminate# traceLog "======= 結束執行runstats $p_tabNm ======" ""#}####*********************************************************************## 功能描述:執行sql,執行後將返回成功失敗標誌,不提交數據庫## 數據流向:無## 參數說明:## sqlStr: 表示將執行的sql## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## -1 執行失敗## 0 執行成功##*********************************************************************#proc exeSqlNoCommit {p_sqlStr p_errCode} {# set rt [ _exeSql $p_sqlStr $p_errCode 1 0 ]# return $rt#}##*********************************************************************## 功能描述:激活不記錄日誌開關## 數據流向:無## 參數說明:## table_str: 表示激活不記錄日誌開關的目標表## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## -1 執行失敗## 0 執行成功##*********************************************************************#proc notLogInit {table_str p_errCode} {# set p_sqlStr "alter table ${table_str} activate not logged initially"# set rt [ _exeSql $p_sqlStr $p_errCode 1 0 ]# return $rt#}###*********************************************************************## 功能描述:清空表同時不記錄日誌## 數據流向:無## 參數說明:## table_str: 表示須要清空的目標表## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## -1 執行失敗## 0 執行成功##*********************************************************************#proc truncatTab {table_str p_errCode} {# set p_sqlStr "alter table ${table_str} activate not logged initially with empty table"# set rt [ _exeSql $p_sqlStr $p_errCode 1 1 ]# return $rt#}##*********************************************************************## 功能描述:去年同期## 數據流向:無## 參數說明:字符串(數字字符串)## 返回值說明:## n 條數## -1 獲取時出錯##*********************************************************************#proc getLastYearPeriod {p_string} {# global conn# global handle## # 檢查表# set sqlstat "select date('$p_string') - 1 Year from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return $v# }#}###*********************************************************************## 功能描述:刪除表## 數據流向:無## 參數說明:## table_str: 表示須要刪除的目標表## errCode: 表示sql執行的返回碼,在一個程序裏面應該惟一(遞增),用於惟一標示執行的sql#### 返回值說明:## -1 執行失敗## 0 執行成功##*********************************************************************#proc droptab {table_str p_errCode} {## set isExist [ isTabExist ${table_str} ]# if {$isExist == 0} {# return 0# }## set p_sqlStr "alter table ${table_str} activate not logged initially with empty table"# set rt [ _exeSql $p_sqlStr $p_errCode 1 1 ]## if {$rt == -1} {# return -1# }## set p_sqlStr "drop table ${table_str}"# set rt [ _exeSql $p_sqlStr $p_errCode 1 1 ]# return $rt#}###*********************************************************************## 功能描述:獲取某個日期對應DT表的表名,若當日表,當月表,日期對應周表都不存在則返回-1## 數據流向:無## 參數說明:## table_str: DT表的表名,例如 DW_NB_GPRS_DT## op_time: 傳入的數據日期,格式yyyy-MM-dd#### 返回值說明:## "相關的日月周表中都不存在${op_time}日數據00檢查生成此表${table_str}的tcl是否成功執行"## 其它字符串 表示表名 例如:DW_NB_GPRS_DT_2/DW_NB_GPRS_DT/DW_NB_GPRS_DT_201203##*********************************************************************#proc getTabName {table_str op_time} {# ##獲取當前數據月最後一天日期# set lde_date [ getLastMday ${op_time} ]# set sde_date [ formatDate ${op_time} "yyyy-mm-dd" "yyyymmdd" ]# set sm_date [ formatDate ${op_time} "yyyy-mm-dd" "yyyymm" ]## set query_tab "${table_str}"## set sqlbuf "(select 1 from ${query_tab} where OP_TIME='${op_time}' fetch first 5 rows only) as T"# set recnum [ getTabRecNum ${sqlbuf} ]## if { ${recnum} <= 0 } {# ##traceLog "-----${query_tab} 記錄條數不對,返回條數=${recnum}--下面檢查是不是月末日期,如果月末日期,則進行檢查月表,不然檢查周表--" ""# set tab_virend [ getWeekDay ${op_time} ]# if { ${op_time} == ${lde_date} } {# set tab_virend ${sm_date}# }# set query_tab "${table_str}_${tab_virend}"## ##traceLog "-----檢查周或月表記錄條數${query_tab} ----" ""# set sqlbuf "(select 1 from ${query_tab} where OP_TIME='${op_time}' fetch first 5 rows only) as T"# set recnum [ getTabRecNum ${sqlbuf} ]# if { ${recnum} <= 0 } {# ##traceLog "---周或月表--${query_tab} 記錄條數不對,返回條數=${recnum}--" ""# set query_tab "|表${table_str}相關${op_time}日數據不存在檢查tcl是否執行|"# }# }# return ${query_tab}#}###*********************************************************************## 功能描述: 獲取當前日期以後若干個月的日期## 數據流向: 無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 2 相隔天數month_num,格式數字,無前導0## 返回值說明:## month_num個月以後的日期,month_num爲負數表示month_num個月以前的日期##*********************************************************************#proc getNextMonthsDay {p_optime month_num} {# global conn# global handle## # 檢查表# set sqlstat "select date('${p_optime}') + (${month_num}) month from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return $v# }#}###*********************************************************************## 功能描述:獲取形式爲 XXXX_yyyyMMdd類型的帶8位日期格式的表對應表名上的最新數據日期## 參數說明:## p_tabName: 傳入的表的除表名上數據日期外的前面一部分字符## 返回值說明:## 返回表名上的最大日期,若查詢時候sql報錯,則返回傳入數據日期## 日期格式爲:yyyyMMdd##*********************************************************************#proc getTabMaxDate {p_tabName p_optime} {# global conn# global handle# set sd_date [ formatDate ${p_optime} "yyyy-mm-dd" "yyyymmdd" ]# set sqlbuf "select max(right(tabname,8)) as sd_date# from syscat.tables# where tabname like upper('${p_tabName}%')# and length(trim(tabname))=(length('${p_tabName}')+8 )# and right(tabname,8)>='${sd_date}' with ur"# traceLog "======= 開始執行SQL語句 =======" ""# traceLog ${sqlbuf} ""## if [catch {aidb_sql $handle $sqlbuf} errmsg] {# trace_sql $errmsg 999# return $sd_date# } else {# set sd_date [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return $sd_date# }# traceLog "======= 執行SQL語句 結束=======" ""#}####*********************************************************************## 功能描述:獲取形式爲 XXXX_yyyyMMdd類型的帶8位日期格式的表對應表名上的最近數據日期## 參數說明:## p_tabName: 傳入的表的除表名上數據日期外的前面一部分字符## 返回值說明:## 返回表名上的最大日期,若查詢時候sql報錯,則返回最新的系統時間-1的 日期## 日期格式爲:yyyyMMdd##*********************************************************************#proc getTabRecentDate {p_tabName p_optime} {# set sd_date [ formatDate ${p_optime} "yyyy-mm-dd" "yyyymmdd" ]# set lde_date [ getLastMday ${p_optime} ]# set sde_date [ formatDate ${lde_date} "yyyy-mm-dd" "yyyymmdd" ]# set sdf_date [ formatDate ${p_optime} "yyyy-mm-dd" "yyyymm" ]# set sdf_date "${sdf_date}01"# global conn# global handle# set sqlbuf "select max(right(tabname,8)) as sd_date# from syscat.tables# where tabname like upper('${p_tabName}%')# and length(trim(tabname))=(length('${p_tabName}')+8 )# and right(tabname,8)<='${sde_date}'# and right(tabname,8)>='${sdf_date}'# with ur"# traceLog "======= 開始執行SQL語句 =======" ""# traceLog ${sqlbuf} ""## if [catch {aidb_sql $handle $sqlbuf} errmsg] {# trace_sql $errmsg 999# return $sd_date# } else {# set sd_date [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return $sd_date# }# traceLog "======= 執行SQL語句 結束=======" ""#}#####*********************************************************************## 功能描述:獲取形式爲 XXXX_yyyyMMdd類型的帶8位日期格式的表對應數據日期的表,獲取不到當日的表就取當前日期對應的月底表,若未到月底則獲取最新的數據表## 參數說明:## p_tabName: 傳入的表的除表名上數據日期外的前面一部分字符## p_optime: 數據日期## ismaxdate: 在獲取不到當日表或者月底表的時候,是否獲取最新的表.1,獲取最新的表;0,不獲取最新的表## 返回值說明:## 返回傳入數據日期對應的數據表,若獲取不到則取月底表,若月底日期大於當前系統日期-1,則獲取最新日期的數據表,## 若最新的數據日期小於當前數據日期對應的月初01號,則報錯返回數據日期對應的表## 日期格式爲:yyyyMMdd##*********************************************************************#proc getRecentTab {p_tabName p_optime ismaxdate} {## set lde_date [ getLastMday ${p_optime} ]# set sd_date [ formatDate ${p_optime} "yyyy-mm-dd" "yyyymmdd" ]# set sde_date [ formatDate ${lde_date} "yyyy-mm-dd" "yyyymmdd" ]### set var_tab "${p_tabName}${sd_date}"# set isExist [ isTabExist ${var_tab} ]# ###若數據日期對應的表不存在,則進行獲取月底日期的表# if { ${isExist} == 0 } {# ##取系統時間# set sys_ld_date [ exec date +%Y-%m-%d ]# ##取系統時間的前一天當作系統時間,方便後面作判斷處理# set sys_ld_date [ getPreDay ${sys_ld_date} ]# set sys_sd_date [ formatDate ${sys_ld_date} "yyyy-mm-dd" "yyyymmdd" ]## ###若數據日期對應月末日期小於系統時間-1,則返回月末日期對應的數據表# if { ${sde_date} < ${sys_sd_date} } {# set var_tab "${p_tabName}${sde_date}"# set isExist [ isTabExist ${var_tab} ]# if { ${isExist} == 0 } {# if { ${ismaxdate} == 1 } {# set max_sd_date [ getTabMaxDate ${p_tabName} ${p_optime} ]# } else {# set max_sd_date [ getTabRecentDate ${p_tabName} ${p_optime} ]# }# set var_tab "${p_tabName}${max_sd_date}"# }# } else {# set max_sd_date [ getTabRecentDate ${p_tabName} ${p_optime} ]# set var_tab "${p_tabName}${max_sd_date}"# }# }## return ${var_tab}##}######*********************************************************************## 功能描述:獲取前數月同期## 數據流向:無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd## 2 多少月 數字## 返回值說明:## qianxl add 20140819##*********************************************************************#proc getNMonPeriod {p_string p_cnt} {# global conn# global handle## # 檢查表# set sqlstat "select date('$p_string') - ${p_cnt} month from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return $v# }#}###*********************************************************************## 功能描述: 獲取上個季度## 數據流向: 無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd#### 返回值說明:## 返回上個季度年及在當年屬於第幾季度## qianxl add 20140819##*********************************************************************#proc getPreseason {p_optime} {# global conn# global handle## # 檢查表# set sqlstat "select date('${p_optime}') -3 month from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# scan $v "%04s-%02s-%02s" tyear tmonth tday## set sqlstat "select int(ceiling(${tmonth}*1.00/3)) from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# # 返回日期# return ${tyear}${v}# }## }#}####*********************************************************************## 功能描述: 獲取上個季度三個月份## 數據流向: 無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd#### 返回值說明:## 返回上季度的三個月份## qianxl add 20140819##*********************************************************************#proc getPreSMon {p_optime} {# global conn# global handle## # 檢查表# set sqlstat "select date('${p_optime}') -3 month from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# scan $v "%04s-%02s-%02s" tyear tmonth tday## set sqlstat "select int(ceiling(${tmonth}*1.00/3)) from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]## # return $v# if { $v == 1 } {# return ${tyear}01-${tyear}02-${tyear}03# }# if { $v == 2 } {# return ${tyear}04-${tyear}05-${tyear}06# }# if { $v == 3 } {# return ${tyear}07-${tyear}08-${tyear}09# }# if { $v == 4 } {# return ${tyear}10-${tyear}11-${tyear}12# }# }# }#}####*********************************************************************## 功能描述: 獲取本季度三個月份## 數據流向: 無## 參數說明:## 1 基準日期p_optime,格式yyyy-mm-dd#### 返回值說明:## 返回本季度的三個月份## qianxl add 20140819##*********************************************************************#proc getTheSMon {p_optime} {# global conn# global handle## scan $p_optime "%04s-%02s-%02s" tyear tmonth tday## set sqlstat "select int(ceiling(${tmonth}*1.00/3)) from information_schema.SYS_SCN"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]## # return $v# if { $v == 1 } {# return ${tyear}01-${tyear}02-${tyear}03# }# if { $v == 2 } {# return ${tyear}04-${tyear}05-${tyear}06# }# if { $v == 3 } {# return ${tyear}07-${tyear}08-${tyear}09# }# if { $v == 4 } {# return ${tyear}10-${tyear}11-${tyear}12# }# }##}##*********************************************************************## 功能描述: 獲取最新數據表## 數據流向: 無## 參數說明:## 表名#### 返回值說明:## 返回該表的最新數據日期## qianxl add 20141120##*********************************************************************#proc getnewTab {Tab_name} {# global conn# global handle## set sqlstat "select tabname from syscat.tables where tabname like '${Tab_name}%' order by 1 desc fetch first 1 rows only with ur"# if [catch {aidb_sql $handle $sqlstat} errmsg] {# trace_sql $errmsg 999# # 若是語句執行出錯,返回-1# return -1# } else {# set v [ aidb_fetch $handle ]# aidb_commit $conn# aidb_close $handle# set handle [aidb_open $conn]# return $v## }##}####*********************************************************************## 功能描述:去年同期 new## 數據流向:無## 參數說明:字符串(數字字符串)## 返回值說明:## n 條數## -1 獲取時出錯##*********************************************************************#proc getLastYearPeriod1 {p_optime} {# set year 0# set month 0# set day 0# scan $p_optime "%04d-%02d-%02d" year month day## set year [expr $year -1]## #如果1月# if { $month == 2 && $day ==29 } {# #返回去年2月最後一天# set day [ getMonthDays ${year}-${month}-01 ]# }## set month [format "%02d" $month]# set day [format "%02d" $day]## return $year-$month-$day#}