当前位置: 代码迷 >> PB >> sql 中 where 条件有关问题
  详细解决方案

sql 中 where 条件有关问题

热度:44   发布时间:2016-04-29 09:43:51.0
sql 中 where 条件问题
大家给看一下,PB的SQL语句错误,不知如何处理?
string cur_hh,cur_cpmc,cur_gg,cur_dw,cur_sjr,cur_sjfs
string sql_where //筛选条件
int rec_count //符合条件记录数
decimal cur_ylhj,cur_flhj,cur_jghj //原料成本合计、辅料成本合、加工成本合计
sql_where="cpbh=:cur_cpmc and dw=:cur_dw "
if not isnull(sle_1.text) then
cur_hh=sle_1.text
sql_where=sql_where + "and hh=:cur_hh"
end if
if not isnull(sle_3.text) then
cur_gg=sle_3.text
sql_where=sql_where + "and gg=:cur_gg"
end if
if not isnull(sle_2.text) then
cur_sjr=sle_2.text
sql_where=sql_where + "and sjr=:cur_sjr"
end if
if not isnull(ddlb_1.text) then
cur_sjfs=ddlb_1.text
sql_where=sql_where + "and sjfs=:cur_sjfs"
end if


select count(*) into :rec_count from wcb_bjcb where ":sql_where" ;

最后一句提示在关键字'set '附近有语法错误!

------解决方案--------------------
完全不符合pbsql语法,得用动态sql了

string cur_hh,cur_cpmc,cur_gg,cur_dw,cur_sjr,cur_sjfs 
string sql_where //筛选条件 
int rec_count //符合条件记录数 
decimal cur_ylhj,cur_flhj,cur_jghj //原料成本合计、辅料成本合、加工成本合计 
sql_where="cpbh='"+cur_cpmc+"' and dw='"+cur_dw +"'" 
if not isnull(sle_1.text) then 
cur_hh=sle_1.text 
sql_where=sql_where + "and hh='"+cur_hh+"'" 
end if 
if not isnull(sle_3.text) then 
cur_gg=sle_3.text 
sql_where=sql_where + "and gg='"+cur_gg+"'" 
end if 
if not isnull(sle_2.text) then 
cur_sjr=sle_2.text 
sql_where=sql_where + "and sjr='"+cur_sjr+"'" 
end if 
if not isnull(ddlb_1.text) then 
cur_sjfs=ddlb_1.text 
sql_where=sql_where + "and sjfs="+cur_sjfs
end if 

string ls_sql
long ll_count
ls_sql = "select count(*) from wcb_bjcb where "+sql_where ; 
DECLARE my_cursor DYNAMIC CURSOR FOR SQLSA ;
PREPARE SQLSA FROM ls_sql ;
OPEN DYNAMIC my_cursor ;
FETCH my_cursor INTO :ll_count;
CLOSE my_cursor ;
  相关解决方案