if edit1.Text<>'' then a:=''''+edit1.Text+'''';
if edit2.Text<>'' then b:=''''+edit2.Text+'''';
if edit3.Text<>'' then c:=''''+edit3.Text+'''';
少了一些‘'’

解决方案 »

  1.   

    datamodule2.ADOQuery2.sql.add('exec query2 @a='''
    +a+
    ''',@b='''+b+
    ''',@c='''+c+
    '''');
      

  2.   

    var
      strSQL: String;
    begin
      strSQL := 'exec query2 ';
      if if edit1.Text<>'' then
        strSQL := strSQL + ' a = '''+edit1.Text+''','
      if if edit2.Text<>'' then
        strSQL := strSQL + ' a = '''+edit2.Text+''','
      if if edit3.Text<>'' then
        strSQL := strSQL + ' a = '''+edit3.Text+''','
      if strSQL[Length(strSQL)] = ',' then
         strSQL := Copy(strSQL,1,Length(strSQL) - 1);datamodule2.ADOQuery2.close;
    datamodule2.ADOQuery2.sql.clear;
    datamodule2.ADOQuery2.sql.add(strSQL);
    datamodule2.ADOQuery2.Open;
      

  3.   

    create proc query2
    (
    @a varchar(20)=null,
    @b varchar(20)=null,
    @c varchar(20)=null
    )
    as
    begin
    declare @sSQL varchar(200)
            select @sSQL = 'select * from basicinfo where 1 = 1 '
    if @a is not null
    select @sSQL = ' and name1 like '%''+ @a +''%''
    if @b is not null
    select @sSQL = ' and name2 like '%''+ @b +''%''
    if @c is not null
    select @sSQL = ' and name3 like '%''+ @c +''%''
            execute(@sSQL)end