案例:有一个业务需要同时更新两个或以上的表
分析:
同时更新两个表,简单一点的就是用其中一个表做DW更新,同时用SQL更新另外一个表;或者页trigger实现其他表的更新。但这样对于相类似的业务就无疑是大大增加工作量。而且维护不方便。
但我们细细考虑一下,DW作为PB的利器自然有他独到的地方。更新属性就好像是为了这种多表更新而设计的。
PB更新数据库的时候首先看更新的table,然后看需要更新的column。也就是说,只需要在更新的时候设置好更新属性,就可以方便的实现多表更新了。
步骤可以简单理解为:更新的时候将表1作为可更新表,表1的column作为可更新column。更新完后再设置表2作为可更新表,表2的column作为可更新column。
实例:
假设有tabletbl_1(col_1, col_2), tbl_2(col_1, col3)
那么,DW的SQL就是
select tbl_1.col_1,
tbl_1.col_2,
tbl_2.col_3
from tbl_1, tbl_2
where tbl_1.col_1 = tbl_2.col_1
当DW作了一系列操作后,在更新的时候执行以下script
long ll_cnt = 0
int i = 0
string ls_table[2] = {'tbl_1', 'tbl_2‘}
string ls_col
string ls_col_dbname
string ls_update
string ls_updateable
string ls_error
boolean lb_update
// 循环table列表,实现多表更新
for i = 1 to upperBound(ls_table)
// 循环dw的column
for ll_cnt = 1 to long(dw_1.describe("DataWindow.Column.Count"))
ls_col = dw_1.describe("#" + string(ll_cnt) + ".name")
ls_col_dbname = dw_1.describe("#" + string(ll_cnt) + ".dbname")
// 如果属于更新表的column或者key,设为可更新column
// 这里也可以将公共column也列作可更新column
if pos(ls_col_dbname, ls_table + ".") = 1 or &
lower(dw_1.describe("#" + string(ll_cnt) + ".key")) = "yes" then
ls_updateable = ".update=yes"
else
ls_updateable = ".update=no"
end if
ls_update += "~t" + ls_col + ls_updateable
next
// 设置更新table
ls_update += "~tDataWindow.Table.UpdateTable='" + ls_table[i] + "'"
// 更新设置无误后update
ls_error = dw_1.modify(ls_update)
if trim(ls_error) <> '' or ls_error <> '!' then
if dw_1.update(true, false) = 1 then
lb_update = true
else
lb_update = false
// 退出for循环
exit
end if
end if
nex
// 此处根据更新情况提交事务
if lb_update then
commit;
// 重设dw状态
dw_1.resetupdate()
else
rollback;
end if
//到这里就完成了多表更新了
分析:
同时更新两个表,简单一点的就是用其中一个表做DW更新,同时用SQL更新另外一个表;或者页trigger实现其他表的更新。但这样对于相类似的业务就无疑是大大增加工作量。而且维护不方便。
但我们细细考虑一下,DW作为PB的利器自然有他独到的地方。更新属性就好像是为了这种多表更新而设计的。
PB更新数据库的时候首先看更新的table,然后看需要更新的column。也就是说,只需要在更新的时候设置好更新属性,就可以方便的实现多表更新了。
步骤可以简单理解为:更新的时候将表1作为可更新表,表1的column作为可更新column。更新完后再设置表2作为可更新表,表2的column作为可更新column。
实例:
假设有tabletbl_1(col_1, col_2), tbl_2(col_1, col3)
那么,DW的SQL就是
select tbl_1.col_1,
tbl_1.col_2,
tbl_2.col_3
from tbl_1, tbl_2
where tbl_1.col_1 = tbl_2.col_1
当DW作了一系列操作后,在更新的时候执行以下script
long ll_cnt = 0
int i = 0
string ls_table[2] = {'tbl_1', 'tbl_2‘}
string ls_col
string ls_col_dbname
string ls_update
string ls_updateable
string ls_error
boolean lb_update
// 循环table列表,实现多表更新
for i = 1 to upperBound(ls_table)
// 循环dw的column
for ll_cnt = 1 to long(dw_1.describe("DataWindow.Column.Count"))
ls_col = dw_1.describe("#" + string(ll_cnt) + ".name")
ls_col_dbname = dw_1.describe("#" + string(ll_cnt) + ".dbname")
// 如果属于更新表的column或者key,设为可更新column
// 这里也可以将公共column也列作可更新column
if pos(ls_col_dbname, ls_table + ".") = 1 or &
lower(dw_1.describe("#" + string(ll_cnt) + ".key")) = "yes" then
ls_updateable = ".update=yes"
else
ls_updateable = ".update=no"
end if
ls_update += "~t" + ls_col + ls_updateable
next
// 设置更新table
ls_update += "~tDataWindow.Table.UpdateTable='" + ls_table[i] + "'"
// 更新设置无误后update
ls_error = dw_1.modify(ls_update)
if trim(ls_error) <> '' or ls_error <> '!' then
if dw_1.update(true, false) = 1 then
lb_update = true
else
lb_update = false
// 退出for循环
exit
end if
end if
nex
// 此处根据更新情况提交事务
if lb_update then
commit;
// 重设dw状态
dw_1.resetupdate()
else
rollback;
end if
//到这里就完成了多表更新了