在使用Datastage Parallel开发事实表的过程中,经常会出现因为脏数据、重置某日的日全量数据,而需要重跑数据的情况。这就需要在run job之前首先调用数据库的命令,清除不再需要的数据,以保证后续加载程序的成功运行。如果是单张表的操作,可以直接在前置子进程中,调用shell命令。如果是一个job操作多张表的情况,可以通过开sequential job,通过execute command控件执行。
为了避免开发额外的sequential job,可以通过routine程序多次调用不同sql command.
我使用的环境是这样的,有开发好的call db command的shell程序,只需要在routing中多次调用。同时DB的连接参数是存在DS的参数集里面的。基于以上的条件开发routine程序
*************************************************************************
**ROUTINE名称:DSJCallDBCommand
**ROUTINE类型:Server Subroutine
**ROUTINE功能:执行多条db2命令
**ROUTINE作者:Mike Peng
**ROUTINE日期:20140801
**注意事项:
** 1.避免对同一张表的同时操作
** 2.参数使用";"作为分隔符,且末尾需要加";"
** 3.参数使用方法 db_instance;db_user;db_password;"SQL Statement1";"SQL Statement2";....;
*************************************************************************
*$INCLUDE DSINCLUDE JOBCONTROL.H
ErrorCode = 0 ; * 将该项设置为非零值可停止阶段/作业
delimeter=";" ; * 设置参数分隔符
Call DSLogInfo(InputArg,"DSJCallDBCommand")
if InputArg="" then
Ans="Params should not be empty!"
Call DSLogWarn(Ans,"DSJCallDBCommand")
* ErrorCode = 1
end
else
trimDelimeter=EReplace(InputArg, ";", "")
paramcnt= Len(InputArg)-Len(trimDelimeter) ; *计算参数个数
statementcnt=paramcnt - 3
message="此前置程序需要执行":statementcnt:"个SQL."
Call DSLogInfo(message,"DSJCallDBCommand")
cmdstr="sh /etldata/script/shellscript/call_db2_cmd.sh "
num=1
Loop
While num <= 3 do
if num=1 then
start = 0
end
else
start=index(InputArg,delimeter,num-1)
end
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstr:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
num=num+1
repeat
cmdstrtmp=cmdstr
num=4
Loop
While num <= paramcnt do
start=index(InputArg,delimeter,num-1)
position=index(InputArg,delimeter,num)
comm=InputArg[start+1,position - start-1]
cmdstr=cmdstrtmp:" ":comm
call DSLogInfo(cmdstr,"DSJCallDBCommand")
statementN0= num-3
message1="Prepared SQL_":statementN0:cmdstr
message2="Excuting SQL_":statementN0:cmdstr
Call DSLogInfo(message1,"DSJCallDBCommand")
Call DSExecute("UNIX",cmdstr,Ans,SystemReturnCode)
Call DSLogInfo(message2,"DSJCallDBCommand")
num=num+1
REPEAT
END
调用此进程是,参数输入为:#PARAMS_DB.$DB_SID#;#PARAMS_DB.$DBUSER#;#PARAMS_DB.$DBPWD#;"SQL Statement1";"SQL Statement2";
图例为2个sql 语句,可以在参数后端继续添加sql命令