sql語句中添加查詢條件

/**************************************************** 函數描述: 在strsql最後一個From 後的最後一個 where後添加條件strwhere     在最後一個From後如果有where,條件加在where後;如果沒有where,     在條件前加where再加條件函數參數: strsql:      原sql;           strwhere:    條件,      函數返回:  生成的帶查詢條件的sql

函數完成:  woods 日    期:  2006-03-30

注:如果strsql中不存在where,但是存在group by或having此函數不可用!   如果存在上述sql,可以添加一個永遠成立的條件(如where 1=1),再使用此函數   生成sql *****************************************************/

string strnewsql,sql,sqlwhere,sqlsort,strAfterLastFormSql string strsql1,strsql2,strsql3 long i,j,possort,poswhere,posFrom

strsql1=strsql strsql1=upper(strsql1)

//查詢最後一個From posFrom=len(strsql1)+1 for i=1 to len(strsql1)  j=pos(strsql1,'FROM',i)  if j>0 then //如果存在“from”,繼續查找以後的sql   posFrom=j //紀錄位置點   i=j+6  else   exit  end if next

//查詢From後最後一個Where poswhere=len(strsql1)+1 for i=posFrom+6 to len(strsql1)  j=pos(strsql1,'WHERE',i)  if j>0 then //如果存在“where”,繼續查找以後的sql   poswhere=j //紀錄位置點   sqlwhere=mid(strsql1,j)   i=j+7  else   exit  end if next

//得到沒有條件、排序的sql sql=left(strsql1,poswhere - 1)

//生成where:sql if strwhere<>'' and isnull(strwhere)=false then  if sqlwhere='' or isnull(sqlwhere)=true then   sqlwhere=" where "+strwhere  else   sqlwhere=' where ('+strwhere+') and '+right(sqlwhere,len(sqlwhere) - 5)  end if end if

strnewsql=sql+sqlwhere+sqlsort

return strnewsql

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章