//表名,关键字段名,单条内容的SQL语句,产生新记录的值 function Tfrmdmmain.CopyTbale(const tablename, fileldname, swhere, newfileldstring: string): integer; var sql,sqlstring,sqlvalue,newsqlvalue:string; i:integer; begin sql := 'INSERT INTO [%s] (%s) VALUES(%s)'; self.fykExecute.Close ; self.fykExecute.SQL.Text := swhere; self.fykExecute.Open ; //取得字段资料 ,注意这里进行字段类型的判断,及NULL的处理。针对SQLSERVER sqlstring:=StringReplace(Trim(fykExecute.FieldList.Text), #13#10, ',', [rfReplaceAll]); for i := 0 to fykExecute.FieldCount -1 do begin if fykExecute.Fields[i].FieldName = fileldname then begin sqlvalue :=sqlvalue + newfileldstring +','; end else begin if fykExecute.Fields[i].DataType in [ftString , ftDate, ftTime, ftDateTime, ftMemo, ftFmtMemo, ftWideString] then begin if fykExecute.Fields[i].AsString <>'' then begin sqlvalue :=sqlvalue +''''+ fykExecute.Fields[i].AsString +''','; end else begin sqlvalue :=sqlvalue +'NULL'+','; end; end else begin if fykExecute.Fields[i].AsString <>'' then begin sqlvalue :=sqlvalue + fykExecute.Fields[i].AsString +','; end else begin sqlvalue :=sqlvalue +'NULL'+','; end; end; end; end; newsqlvalue:= copy(sqlvalue,0,length(sqlvalue)-1) ; // showessage(sqlvalue); self.fykExecute.Close ; self.fykExecute.SQL.Text := format(sql,[tablename,sqlstring,newsqlvalue]); fykExecute.SQL.SaveToFile('C:1.TXT'); Resuntl := self.fykExecute.ExecSQL ; end;