pb 动态改变DW的WHERE子句

PB心得4-动态改变DW的WHERE子句

在DW的FILTER的使用一文中提到如果检索条件因素不固定的话,可以使用动态改变DW的WHERE子句的方法。

这个方法的实现是这样的,做一个没有参数的DW,在程序中,先通过DW.OBJECT.DATAWINDOW.TABLE.SELECT来取得DW对象的SQL语法,可以对SQL语法拆分出SELECT部分,WHRER部分,GROUP BY部分,HAVING部分。如果是UNION ALL 那么应该需要用数组变量来接收对应的部分,这些变量最好是用环境变量。

再做个用于查询条件的DW,里面是你可以让用户输入查询条件的字段项目。然后在查询按钮的脚本下做如下面的步骤,读取查询条件的字段对应的条件值,对每个条件值做判断,如果是不为空的才需要生成对应的条件语法,根据字段的不同,合成条件语法时注意引号和类型转换。条件之间的逻辑,运算优先级等。

合成后的整个新的条件语法与原来的SQL语法的WHRER部分合并,再与其他的SQL的部分合并成完整的新的条件的SQL语法。

把新的SQL语法通过DW.OBJECT.DATAWINDOW.TABLE.SELECT传回给DW,并运行检索函数。

string ls_odd
string ls_provider
string ls_receiver
string ls_oddtype
datetime ldt_start,ldt_end
string ls_stockcode
string ls_stockname
string ls_sql
string ls_where,ls_where1
string ls_type1
string ls_type2
string ls_cid
datawindowchild ldwc_1
dw_inquire.accepttext()
string ls_sql1
ls_sql  = mid(is_sql,1,pos(is_sql,‘union all‘) - 1)
ls_sql1 = mid(is_sql,pos(is_sql,‘union all‘) + 9,len(is_sql) - pos(is_sql,‘union all‘) +1)

ls_odd = dw_inquire.object.oddno[1]
ls_provider = dw_inquire.object.provider[1]
ls_receiver = dw_inquire.object.receiver[1]
ls_oddtype = dw_inquire.object.oddtype[1]
ls_stockcode = dw_inquire.object.stockcode[1]
ls_stockname = dw_inquire.object.stockname[1]
ldt_start = dw_inquire.object.dstart[1]
ldt_end = dw_inquire.object.dend[1]
ls_type1 = dw_inquire.object.type1[1]
ls_type2 = dw_inquire.object.type2[1]
ls_cid = dw_inquire.object.cid[1]
if pos(lower(is_sql),‘where‘)>0 then
 ls_where = ‘      ‘
 ls_where += " and out_main.auditing = ‘"+ls_type2+"‘ "
else 
 ls_where = ‘ where ‘
 ls_where += " out_main.auditing = ‘"+ls_type2+"‘ "
end if
if pos(lower(ls_sql1),‘where‘)>0 then
 ls_where1 = ‘      ‘
 ls_where1 += " and group_out.auditing = ‘"+ls_type2+"‘ "
else 
 ls_where1 = ‘ where ‘
 ls_where1 += "  group_out.auditing = ‘"+ls_type2+"‘ "

end if
if not(isnull(ls_odd) or len(trim(ls_odd)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.odd_no like ‘%" + ls_odd + "%‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " group_out.odd_no like ‘%" + ls_odd + "%‘ "
end if

if not(isnull(ls_provider) or len(trim(ls_provider)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.provider = ‘" + ls_provider + "‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " group_out.provider = ‘" + ls_provider + "‘ "
end if

if not(isnull(ls_receiver) or len(trim(ls_receiver)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.receiver= ‘" + ls_receiver + "‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " group_out.receiver = ‘" + ls_receiver + "‘ "
end if

if not(isnull(ls_oddtype) or len(trim(ls_oddtype)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.receiver_provider_type= ‘" + ls_oddtype + "‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " group_out.receiver_provider_type= ‘" + ls_oddtype + "‘ "
  
end if

if not(isnull(ls_stockcode) or len(trim(ls_stockcode)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  //if len(ls_stockcode) = 5 then
 ls_where += " out_child.pid = " + ls_stockcode + " "
 // else
 //  ls_where += " out_child.stock_code like ‘" + ls_stockcode + "%‘ "
 //end if
 if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " 1 = 2 "
end if

if not(isnull(ls_stockname) or len(trim(ls_stockname)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_child.stock_name like ‘%" + ls_stockname + "%‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " 1=2 "
end if

if not(isnull(ls_cid) or len(trim(ls_cid)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_child.cid = " + ls_cid + " "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " 1=2 "
end if
if ls_type2 = ‘1‘ then 
 if not(isnull(ldt_start)) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.auditdate >= ‘" + string(ldt_start,‘yyyy-mm-dd‘) +" 00:00:00‘ "
 end if
 
 if not(isnull(ldt_end)) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.auditdate <= ‘" + string(ldt_end,‘yyyy-mm-dd‘) +" 23:59:59‘ "
 end if
else
 if not(isnull(ldt_start)) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.odd_date >= ‘" + string(ldt_start,‘yyyy-mm-dd‘) +" 00:00:00‘ "
 end if
 
 if not(isnull(ldt_end)) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.odd_date <= ‘" + string(ldt_end,‘yyyy-mm-dd‘) +" 23:59:59‘ "
 end if
end if
if not(isnull(ldt_start)) then
 if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
 ls_where1 += " group_out.odd_date >= ‘" + string(ldt_start,‘yyyy-mm-dd‘) +" 00:00:00‘ "
end if

if not(isnull(ldt_end)) then
 if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
 ls_where1 += " group_out.odd_date <= ‘" + string(ldt_end,‘yyyy-mm-dd‘) +" 23:59:59‘ "
end if

if not(isnull(ls_type1) or len(trim(ls_type1)) = 0) then
  if len(ls_where) > 5 then ls_where += ‘ and ‘
  ls_where += " out_main.relate_no like ‘%" + ls_type1 + "%‘ "
  if len(ls_where1) > 5 then ls_where1 += ‘ and ‘
  ls_where1 += " 1=2 "
end if
//messagebox(‘‘,is_sql + ls_where)
dw_print.object.datawindow.table.select = ls_sql + ls_where + ‘ union all ‘ + ls_sql1 + ls_where1
dw_print.settransobject(sqlca)
dw_print.retrieve()
  这种方法最好就是在合成后检查一下合成的语法是否正确,要考虑原SQL语法是否有WHERE条件,如果没有新合成的要加上WHERE关键字。

=========================实践

SQL:

select bill_id,bill_date,org_id,b.memb_id,memb_nm,decode(sex,‘M‘,‘男‘,‘F‘,‘女‘,sex) sex,bill_money,cash_money,card_money,accm_money,coupon_money from
(select bill_id,bill_date,org_id,memb_id,bill_money,cash_money,card_money,accm_money,coupon_money from TB_AX006 t)s,
(select memb_id,memb_nm,sex from tb_ax001 a) b
where s.memb_id = b.memb_id(+)

PB:

String ls_value,ls_sql,ls_where
String ls_dt_str,ls_dt_end
String ls_ddlb_1,ls_sle_1
string ls_ddlb_2

ls_sql=dw_1.OBJECT.DATAWINDOW.TABLE.SELECT
//messagebox("提示",ls_sql)
if pos(lower(ls_sql),‘+‘)>0 then
ls_sql=mid(ls_sql,1,pos(ls_sql,‘+‘) + 1)
//messagebox("截取后sql",ls_sql)
end if

//时间
ls_dt_str = string(dp_1.value,‘yyyy/mm/dd‘)
ls_dt_end = string(dp_2.value,‘yyyy/mm/dd‘)
if dp_1.value > dp_2.value then
messagebox("提示","开始时间不能小于结束时间")
return
end if
ls_where += " and s.bill_date between to_date(‘"+ls_dt_str+"‘,‘yyyy-mm-dd‘) and to_date(‘"+ls_dt_end+"‘,‘yyyy-mm-dd‘) "

//会员
ls_ddlb_1 = trim(ddlb_1.text)
ls_sle_1 = upper(trim(sle_1.text))
IF ls_ddlb_1 = ‘会员姓名‘ THEN
if ls_sle_1 = ‘‘ then
messagebox("提示","请输入会员姓名")
return
else
ls_where += " and upper(b.memb_nm) like ‘%"+ls_sle_1+"%‘ "
end if
ELSEIF ls_ddlb_1 = ‘会员ID‘ THEN
IF ls_sle_1 = ‘‘ THEN
MessageBox("确认", "请输会员ID.")
Return
elseif IsNumber(ls_sle_1) = false then
MessageBox("确认", "输入的不是数字.")
Return
else
ls_where +=" and b.memb_id = ‘"+ls_sle_1+"‘ "
END IF
END IF

//门店
ls_ddlb_2 = trim(ddlb_2.text)
if ls_ddlb_2 = ‘全部‘ then
ls_where += ""
else
ls_where += " and s.org_id =‘"+ls_ddlb_2+"‘"
end if

//messagebox(‘查询sql‘,ls_sql + ls_where)
dw_1.object.datawindow.table.select = ls_sql + ls_where
dw_1.settransobject(sqlca)
dw_1.retrieve()

时间: 2024-08-11 15:34:38

pb 动态改变DW的WHERE子句的相关文章

javascript生成表格增删改查 JavaScript动态改变表格单元格内容 动态生成表格 JS获取表格任意单元格 javascript如何动态删除表格某一行

jsp页面表格布局Html代码 <body > <center> <input type="text" value="111" id="mytext"> <table border="1" width="60%" id="mytable"> <tr> <td id="td1">第一行第一列<

unity3d关于怎样动态改变物体 shader

material = new Material(Shader.Find("Custom/Alpha")); material.SetFloat("_Shininess", materialAlpha); void OnRenderImage(RenderTexture source, RenderTexture destination){Graphics.Blit(source, destination, material);} unity3d关于怎样动态改变物体

Unity动态改变src的解决方法

在c#程序(winform,wpf)中可以嵌入unity控件,从而实现三维场景.通过设置unity控件的src属性来设置场景,然而src必须是绝对路径,而且只能在设计器进行设置,不能在代码中动态修改,这在需要运行时动态切换场景的时就悲剧了.当然网上有很多关于Unity 如何动态改变src的方法,其方法是正确的,最大的问题莫过于没有说明代码该放在什么地方,干着急. 下面假设unity控件的文件Unity3dControl.cs,那么打开Unity3dControl.designer.cs,找到函数

WPF 中动态改变控件模板

在某些项目中,可能需要动态的改变控件的模板,例如软件中可以选择不同的主题,在不同的主题下软件界面.控件的样式都会有所不同,这时即可通过改变控件模板的方式实现期望的功能. 基本方法是当用户点击切换主题按钮是加载新的资源字典,并使用新加载的资源字典替代当前的资源字典这时要用到ResourceManager. 假设现有两个不同的资源字典文件Dictionary1.xaml和Dictionary2.xaml存在于Themes文件夹内: 在MainPage中使用其中一个资源字典作为默认样式文件: <Win

js动态改变iframe的高度

js动态改变iframe的高度的写法 〈iframe id="docDetail" width="100%"  height="200" frameborder="0" src="a.htm"〉 〈/iframe〉 而a.html不是固定大小的,这个时候嵌套的iframe就会出现滚动条. 通过js获取iframe的高度,然后动态的改变,这样就不会出现滚动条. 方式一:在jsp的body里增加onload方法

【iOS知识学习】_iOS动态改变TableView Cell高度

在做tableView的时候,我们有时候需要根据cell的高度动态来调整,最近在网上看到一段代码不错,跟大家Share一下. 在 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 类中获取cell的高度: CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX); cell.textLabel.text

iOS 根据键盘的高度动态改变UIView的高度

在我们使用键盘时常常出现键盘挡着视图这种情况,下面我给大家介绍一种方法可以根据键盘的高度来动态改变视图的度使其可以始终在键盘的上边 在这里视图我用TextView UIKeyboardWillShowNotification//键盘弹出 UIKeyboardWillHideNotification//键盘缩回 //用通知监听键盘的弹出 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboar

动态改变ComboBox下拉框的宽度

在C++Builder中有时下拉框的内容比较长,标准长度下根本显示不完,可以调用PostMessage()方法来实现 ::PostMessage(comb->Handle,CB_SETDROPPEDWIDTH,newwidth,0);//comb是一个ComboBox指针,newwidth是下拉框的新长度 这种方法在下面的情况下会失效1.动态改变了窗体的BorderStyle属性2.动态改变了窗体的Parent属性 摘自: http://showmealone.blog.sohu.com/760

动态改变 网页的宽度 &lt;--&gt; body的滚动条

1. <body style="min-width:600px;overflow:scroll;" allowfullscreen="true"> </body> 当网页的宽度 缩小到 比 min-width的值小时,<body/>就会出现 水平滚动条. 2. 动态改变 <body/> 的 min-width属性: js代码:document.body.style.minWidth = 数值 + "px&qu