with datamodule1.AQinsert_conn do
  begin
    close;
    sql.Clear;
    sql.Add('select * from customer_types where cust_id=:v_keyid');
    Parameters.ParamValues['v_keyid']:=datamodule1.ATinfo.FieldValues['keyid'];
    Open;
    execsql;
    if RecordCount<>0 then
      btappend.Enabled:=false
    else
    begin
      close;
      sql.Clear;
      sql.Add('insert into customer_types (types,cust_id) (select detail,:v_keyid from customer_kind where kind=''产品种类'')');
      Parameters.ParamValues['v_keyid']:=datamodule1.ATinfo.FieldValues['keyid'];
      execsql;
    end;
  end;  datamodule1.AQtypes.Requery();
上面这段语句,要实现的功能是将customer_kind中的产品种类与主表中的主键一起插入到从表customer_types中的功能,其中我是将主表atinfo中的主键keyid赋值给变量v_keyid,再通过insert语名来实现的。
请大家帮我看看,行不行?或者有没有其他方法来实现这个功能?

解决方案 »

  1.   

    close;
        sql.Clear;
        sql.Add('select * from customer_types where cust_id=:v_keyid');
        Parameters.ParamValues['v_keyid']:=datamodule1.ATinfo.FieldValues['keyid'];
        Open;
        execsql;  //open以后可以execsql吗!!!! 当然错了
      

  2.   

    上面这些语句执行下来,我有时会出现syntax error or access violation 这种错误,不知道是什么原因引起的?
      

  3.   

    尽量不要用recordcount,使用isempty
      

  4.   

    只使用一句话      sql.Add('insert into customer_types (types,cust_id) (select detail,:v_keyid from customer_kind where kind=''产品种类'' where :v_keyid1 not exist (select cust_id from customer_types ))');
          Parameters.ParamValues['v_keyid']:=datamodule1.ATinfo.FieldValues['keyid'];
          Parameters.ParamValues['v_keyid1']:=datamodule1.ATinfo.FieldValues['keyid'];
      

  5.   

    sql.Add('select * from customer_types where cust_id=:v_keyid');
        Parameters.ParamValues['v_keyid']:=datamodule1.ATinfo.FieldValues['keyid'];
        Open;
        //execsql;
        if RecordCount<>0 then