lib.tcl

#********************************************************************
# 功能描述:定义公共的函数
# 依赖关系:依赖于全局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#}

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息