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()
?
轉載于:https://www.cnblogs.com/jason-qi/p/4244357.html
總結
以上是生活随笔為你收集整理的pb 动态改变DW的WHERE子句的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bootstrap在线编辑器简单分享
- 下一篇: uva705--slash maze