---------sql生成----------
任务是当用户以逗号来分隔查询的条件,的多条件查询,使用in(x,y),但是当在数据库(access)对应的字段是字符型是就会出现错误,那是当然的!如果是in('x','y')就没有问题!
下面的代码就是对字符串的操作,返回 'x','y'。
'-----接受要处理的字符串-------
function tool_distest(byval textbefore as string)
dim textafter() as string
dim intnum, inttextlen, k, i as integer
intnum = 1
inttextlen = len(textbefore)
'--------计算有多少的查询条件--------
for i = 1 to inttextlen
if mid(textbefore, i, 1) = "," then
intnum = intnum + 1
end if
next
'-----------------------------------
redim textafter(intnum)
k = 1
'--------把每个条件放到数组里---------
for i = 1 to inttextlen
if mid(textbefore, i, 1) = "," then
k += 1
else
textafter(k) = textafter(k) & mid(textbefore, i, 1)
end if
next
'----------------------
dim strtemp as string
'--------为每一个条件加上''----------
for i = 1 to intnum
strtemp = textafter(i)
textafter(i) = "'" & strtemp & "'"
next
'----------------------
strtemp = ""
'------添加分隔条件用的","------
for i = 1 to intnum
strtemp = strtemp & textafter(i) & ","
next
'------------------
'------去除最后一个,------
strtemp = mid(strtemp, 1, len(strtemp) - 1)
'-------------------
return strtemp
end function
最后在sqlserver中又试了试,发现sqlserver查询的时候用in(x,y)来查询字符型是ok的,(sqlserver就是比access强点,呵呵)
---------end-------