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

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

我用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=")

标签:语法,查询,sql
0
投稿

猜你喜欢

  • javascript验证IP地址等验证例子

    2007-09-11 13:40:00
  • css特性:空白外边距互相叠加

    2010-06-21 10:53:00
  • mysql查询一天,查询一周,查询一个月的数据

    2011-01-29 16:22:00
  • asp实现本周的一周时间列表的代码

    2011-04-06 10:45:00
  • 用 jQuery 來做图片伪浮水印

    2010-08-09 12:42:00
  • ASP连接11种数据库语法总结

    2007-09-29 12:07:00
  • ASP+Access系统的安全隐患及对策

    2007-08-23 14:53:00
  • Chrome V8 引擎对 sort 的优化

    2010-02-04 17:27:00
  • 使用 XML 文件记录操作日志

    2008-09-05 17:13:00
  • oracle 集合

    2009-06-19 18:09:00
  • 使用xml http为网站增加域名查询功能

    2008-05-29 13:42:00
  • DNS优化的原理和方法

    2012-04-26 16:43:56
  • SQL Server 数据库操作实用技巧锦集

    2009-01-20 13:20:00
  • 在MySQL数据库中如何来复位根用户的密码

    2008-12-03 16:57:00
  • SQL Server数据库对上亿表的操作

    2008-11-16 18:13:00
  • FCKeditor技巧之在按钮旁边加文字

    2007-10-10 13:17:00
  • 在MySQL中使用XML数据—数据格式化

    2009-12-29 10:26:00
  • 6个asp判断函数使用方法介绍

    2007-09-24 13:10:00
  • 关于基于字体大小(em)的设计

    2008-06-17 15:01:00
  • 给页面 unload 加上效果

    2009-03-12 12:32:00
  • asp之家 网络编程 m.aspxhome.com