你这个函数不就可以嘛,字段名和字段值都可以传进来组合成sql语句呀

解决方案 »

  1.   

    不是的,如果A传过来三个字段,这条语句是可以的但是如果B表传过来的字段还是用
    这个Insert插入呢?c表也是这样一直到n表的
      

  2.   

    哈,这么麻烦啊,既然每张表都不一样,为什么要用同一个函数呢?或者,传递一个动态数组给函数,函数通过数组长度来判断要插入某张表,动态生成sql语句,或者把表名也传递给这个函数,函数中直接通过表名来获取表的结构(有几个字段、主健是什么),在从数组获得参数,你用的是什么数据库?
      

  3.   

    function ff(tblanme,f1name,f2name,... : String) : Boolean;
    begin
    //添加
      Close;
      SQL.Clear;
      SQL.Add('Insert into table'+tblname+'('''+f1name+''','''+f2name+...+'''') values(''ss'',''dd'',....)');
      ExcSQL;
      但是这只是 A表中的字段,如果换成B表的字段该怎么办呢? 总不能再写一条Insert吧! 那我这个函数还有
      什么用呢? update也是这个道理,是这个意思吗?
    end;
      

  4.   

    假设你使用了一个ADOCommand
    那就是
    function ff(ArrayA:Array of string)
    var
      MyADOCommand:TADOCommand;
      I,J:Integer;
      tabName,strSQL:String;
    begin
      I:=length(ArrayA);
      case I of
      2:tabName:='a';
      3:tabName:='b';
      4:tabName:='c';
      end;
      strSQL:='insert into '+tabName;
      for J:=0 to I-1 do
      begin  end;
      
       
    end;
      

  5.   

    假设你使用了一个ADOCommand
    那就是
    function ff(ArrayA:Array of string)
    var
      MyADOCommand:TADOCommand;
      I,J:Integer;
      tabName,strSQL:String;
    begin
      I:=length(ArrayA);
      case I of
      2:tabName:='a';
      3:tabName:='b';
      4:tabName:='c';
      end;
      strSQL:='insert into '+tabName;
      for J:=0 to I-1 do
      begin
        ...........(这个不用写了吧,呵呵)
      end;
      ....
      try
        MyADOCommand.Excute;
      Except
        
      end; 
      MyADOCommand.free; 
       
    end;
      

  6.   

    //参数:
    //TableName  : 字符串, 要操作的表的名字
    //TableRecord: 字符串数组, 存储表中各个字段的字段名
    //RecordValue: 字符串数组, 存储各个字段对应的真实的要插入的值
    //ArrayBund  : 整型, 上面两个数组的元素个数
    //功能:
    //根据传入的两个数组,构造出一个插入的SQL语句,并且执行这条语句,将数据插入表
    function Insert( TableName : string , TableRecord: Array of string ,
                  RecordValue: Array of string , ArrayBound: Integer)
    var 
       strSQL: String ;
       i     : Integer;
    begin
       strSQL = ""
       strSQL = strSQL + "INSERT INTO" + " " + TableName + " ";
       for i:=0 to ArrayBound do
       begin
           strSQL = strSQL + "('" + TableRecord(i) + "')";
       end;
       strSQL = strSQL + " " + "VALUES" ;
       for i:=0 to ArrayBound do
       begin
           strSQL = strSQL + "('" + RecordValue(i) + "')";
       end;
       //这样,strSQL就是插入的SQL语句, 在下面执行 strSQL语句就能插入表中了.
       ....
    end;