插入的时候,某一字段的值是从另外的一个表中读取的! 比如下面代码段中的depid字段,是从dept表中读取的!!
SQLstmt:='insert into "stainfo"'+
 '(stano,sname,sex,depid)VALUES (:InpStano,:InpSname,:InpSex,:depid)
这里的depid该如何替换掉~~ 
注意:在access数据库中~~

解决方案 »

  1.   

    假設你用query1控件來執行這段代碼
    用query2控件來取得depid字段。
    query1.close;
    query1.sql.clear;
    query1.sql.text:=sqlstmt;
    query1.parambyname('depid').value:=query2.fieldbyname('depid').asstring;
    query1.open;注意轉換時的數據類型。還有query2要有數據。
      

  2.   

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      with adoquery2 do
      begin
       //这里用adoquery2查询数据库,从dept表中读取depid字段,并定位到相应记录
      end;
       with ADOQuery1 do
       begin
         Close;
         sql.Clear;
         sql.Add('insert into stainfo(stano,sname,sex,depid) values(:InpStano,:InpSname,:InpSex,:depid)') ;
         Parameters.ParamByName('InpStano').Value :=1 ;
         Parameters.ParamByName('InpSname').Value :='wudi' ;
         Parameters.ParamByName('InpSex').Value :=1 ;
         Parameters.ParamByName('depid').Value :=adoquery2.fieldbyname('depid').value;
         ExecSQL
       end;
    end;
      

  3.   

    谢谢楼上两位,我只想 在 SQL语句中实现,不想 添加 其他控件~~  
    只在SQL语句中,如何实现???
      

  4.   

    我就是想,从表dept中先检索出来这个部门id ,赋值给depid,然后再insert 表stainfo!!我感觉只用SQL应该可以阿!!!
    各位老大,帮帮忙阿~~ 谢谢哈
      

  5.   

    select B表.a,B表.b into A表,这种类似的语句
      

  6.   

    SQLstmt:='insert into "stainfo"'+
     '(stano,sname,sex,depid)VALUES (:InpStano,:InpSname,:InpSex,:depid)
    你没有给条件,所以没法定义到具体的记录,给你,你自己加条件吧SQLstmt:='insert into "stainfo"'+
     '(stano,sname,sex,depid)VALUES (:InpStano,:InpSname,:InpSex,(select depid from dept where 条件))
      

  7.   

    insert into stainfo(stano,sname,sex,depid) select InpStano,InpSname,InpSex,depid from dept
      

  8.   

    TO:Ayx_hys() 
    你的方法中where条件能不能使用变量?? 比如 select id  form dept where dep="&InpDep&"
    InpDep 是变量!!
      

  9.   

    可以的,不过不能那样写,应这样,inpdep作参数使用
    select id  form dept where dep=:InpDep
    在ADOQuery1里调参数