网络编程
位置:首页>> 网络编程>> Asp编程>> “语法错误 (逗号) 在查询表达式id=20, 21”,怎么处理这个逗号?

“语法错误 (逗号) 在查询表达式id=20, 21”,怎么处理这个逗号?

 来源:asp之家 发布时间:2009-09-18 14:52:00 

标签:语法,查询,sql

我用asp+access写程序,   

sql="select   *   from   book   where   id="   
sql=sql   &temp   
  temp是从上个页面传来的显示记录的标号(最多可以选2条记录)   
    
    dim   temp   
      temp=Request("bookchange")   
      if   temp=""   then   
              temp=1   
      end   if   
    
  如果temp传来的是2个数字,我测试了一下,系统报错:   
  错误类型:   
  Microsoft   OLE   DB   Provider   for   ODBC   Drivers   (0x80040E14)   
  [Microsoft][ODBC   Microsoft   Access   Driver]   语法错误   (逗号)   在查询表达式   'id=20,   21'   中。   
  /bookmanage/lendbook.asp,   第   14   行   
    
  怎末处理,请教呀!

用in   
  sql="select   *   from   book   where   id   in   ("   
  sql=sql   &temp   &   ")"   


在sql语句里用in()   
  sql="select   *   from   book   where   id   in   ("&temp&")"


aa=split(temp,',')   
  for   i=0   to   ubound(aa)   
          te=te&"'"&aa(i)&"',"   
  next   
  te=left(te,len(te)-1)   
    
  sql="select   *   from   book   where   id   in   ("&te&")"



StrArr   =   split(temp,   ",")   
  sql="select   *   from   book   where"   
  For   i   =   0   To   UBound(StrArr)   
      StrSQL   =   StrSQL   &   "id   =   '"   &   Trim(StrArr(i))   &   "'"   
      If   i   <   UBound(StrArr)   Then   StrSQL   =   StrSQL   &   "   OR   "   
  Next   
    
  这种应该会比较有效,用in的话,当传过来的是2135,3521的话,会不会连21,13,15等等的记录都加进去呢?没有验证过,不太清楚。

这样就可以了,不用split,不会出错。temp为空的时候要判断一下。   
  最好在提交的时候就规范一下,不要出现只有逗号没有数字的情况。   
  temp=trim(request(temp))   
  if   temp<>""   then   temp="   where   id="   &   temp   
  temp=replac(temp,",","   or   id=")   
  sql="select   *   from   book   "   &   temp   


temp=replac(temp,",","   or   id=")   
    
  这个方法不错   


如果程序运行不正确试试下面的语句:   
  temp=replac(temp,",   ","   or   id=")

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com