[原创]写一个截字串的函数,方便组成SQL字串

在写SQL语句时,我们经常用到如下的语句:

dim collIds

collIds=“2,3,4,5,..........8999“

sql=“delete form  TableA where a.nid in (“ & collIds & “)“

conn.execute sql

''  这时,如果 collIds 太长,我们需要分多次执行才能成功, SQL语名最多才4000行,如果超出去,要失败的.

这时我们可能想这样执行:

sql=“delete form  TableA where a.nid in (1,2,3)“

rs.open sql ,conn,1,1

sql=“delete form  TableA where a.nid in (4,52,7)“

rs.open sql ,conn,1,1

但是如何正确的分隔这些串呢?  我写了一个函数 :

写一个截字串的函数,方便组成SQL字串 
--------------------------------------------------------------------------------
 
在写SQL语句时,我们经常用到如下的语句:

dim collIds

collIds
=2,3,4,5,.8999

sql
=“delete form  TableA where a.nid in (“ & collIds & “)“

conn.execute sql 

''  这时,如果 collIds 太长,我们需要分多次执行才能成功, SQL语名最多才4000行,如果超出去,要失败的.

这时我们可能想这样执行:

sql
=“delete form  TableA where a.nid in (1,2,3)“

rs.open sql ,conn,
1,1

sql
=“delete form  TableA where a.nid in (4,52,7)“

rs.open sql ,conn,
1,1

但是如何正确的分隔这些串呢?  我写了一个函数 :

function mySplit(str,span,maxLenPerEle)
 
dim pos,temp,i
 
dim arr()
 
redim arr(len(str)/maxLenPerEle + 1)
 temp
=str
 i
=0
 
while(len(temp) > maxLenPerEle)
  pos
=instrRev(temp,span,maxLenPerEle,1)
  
if(pos>0then 
   arr(i)
=mid(temp,1,pos-1)
   temp
=mid(temp,pos+1)
   i
=i+1
  
end if
 
wend
 
if(len(temp)>0then
  arr(i)
=temp
 
end if
 
redim preserve arr(i)
 mySplit
=arr
end function 


'' 在使用时,我们这样

 arr
=mysplit(ygbhs,",",2000)
 
for each s in arr
  sql
="delete from table where nid in (" & s & ")"
  conn.execute sql 
 
next


 
posted @ 2006-09-30 10:54  阿牛  阅读(477)  评论(0编辑  收藏  举报