//SQL语句
select * into #xmtemp from xm2002 where month(yeam)='9'
and  xmbh not in (select xmbh from xm2002 where month(yeam)='10') //通过
//变量代入出现问题
procedure Tform1.Button1Click(Sender: TObject);
begin
  with adoquery1 do
  begin
  close;
  sql.clear;
  sql.add('select * into xmtemp from xm2002 where month(yeam)='+''''+inttostr(monthof(datetimepicker1.datetime))+'''') ;//正确
  sql.add('and xmbh not in(select xmbh from xm2002 where month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))+'''') ;//出错
  execsql;
  showmessage('ok');
  end;
end;
出错提示:unclosed quotation before the character string '10'' 
 
//具体成数字是正确的
 procedure TForm1.Button1Click(Sender: TObject);
begin
   with adoquery1 do
   begin
   close;
   sql.clear;
   sql.add('select * into #xmtemp from xm2002 where month(yeam)=''9''') ;
   sql.add('and xmbh not in(select xmbh from xm2002 where month(yeam)=''10'')') ;
   execsql;
   showmessage('ok');
   end;
end;

解决方案 »

  1.   

    select * into #xmtemp from xm2002 where month(yeam)='9'
    and  xmbh not in (select xmbh from xm2002 where month(yeam)='10')怎么写成DELPHI里的语句?
      

  2.   

    '''10'''
    '''9'''
    在DELPHI中'''等于'
      

  3.   

    function QuotedStr(const S: string): string;
      

  4.   

    sql.add('and xmbh not in(select xmbh from xm2002 where month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))+'''') )
    少了一个右括号。                                         ~~这少
      

  5.   

    另外要加空格
      sql.add(' and xmbh not in(select xmbh from xm2002 where month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))+'''')
      

  6.   

    sql.add('and xmbh not in(select xmbh from xm2002 where 
    month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))
    +'''')
     ~~~~~这写错了,该这么写''')'     
     ')
    ~~~      还少了一个右括号和一个单引号。表示这个add方法结束
    这样写太不容易看清了,还不如用一个字符串变量先保存再add呢
    tempsql:='and xmbh not in(select xmbh from xm2002 where 
    month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))+''') ';
    sql.add(tempsql);
      

  7.   

    该在SQL中用引号的地方用#39代替,省得用''',头晕
      

  8.   

    sql.add('and xmbh not in(select xmbh from xm2002 where 
    month(yeam)='+''''+inttostr(monthof(datetimepicker2.datetime))
    +'''')
     ~~~~~这写错了,该这么写''')'  为什么是这么写?没看懂