例子如下,其中在gno是变量为什么执行SQL语句是出错,如何在TQuery组件中执行带变量SQL语句???谢谢
var
gno:string; query1.Close;
 query1.SQL.Clear;
 query1.SQL.Add('select gnum from good where gno = :gno');
 query1.Open;

解决方案 »

  1.   

    要在这一句下面加上给参数gno传值的语句就行了。
    query1.SQL.Add('select gnum from good where gno = :gno');好像是这样写的吧:
    Query1.ParametersByName('gno').value=...
      

  2.   

    if 数据库中gno字段是字符型 then
      'select gnum from good where gno ='''+gno+''''
    else if 数据库中gno字段是整形之类 then
      'select gnum from good where gno = ‘+gno
      

  3.   

    参数是不需要定义的;可以直接引用的,只是你没有给参数赋值肯定要抱错的;
      给你个列子:
       with query1 do
       begin
       close;
       Sql.clear; 
       SQL.Add('select gnum from good where gno = :gno');       
       end;
       query1.parambyname('gno').value;=Edit1.text(你要传给gno的值);
       query1.open;
      

  4.   

    要对:gno参数赋值,否则一定出错
     query1.Close;
     query1.SQL.Clear;
     query1.SQL.Add('select gnum from good where gno = :gno');
     query1.sql.parambyname('gno').assring:=edit1.text; //edit1.text为where语句的参数;
     query1.Open;
    别忘了加分,哈哈哈哈
      

  5.   

    楼上的方法中,只需要在:
    query1.SQL.Add('select gnum from good where gno = :gno');
    gno:=edit1.text;
    不同类型的话可以用:query1.sql.parambyname('gno').assTring:=edit1.text; 
    就可以了哦!
      

  6.   

    query1.Close;
     query1.SQL.Clear;
     query1.SQL.Add('select gnum from good where gno = :gno');
     query1.sql.parambyname('gno').Value:=Mygno;//注:Mygno的类型要和字段gno的类型相同
     query1.Open;
      

  7.   

    query1.Close;
     query1.SQL.Clear;
     query1.SQL.Add('select gnum from good where gno = :gno');
     query1.sql.parambyname('gno').Value:=Mygno;//注:Mygno的类型要和字段gno的类型相同
     query1.prepare;
     query1.Open;query1.param[[0].astring:=xxx;