先用我下面的函数得到字段名,再决定怎么写,大致如下:
sqlString:='Insert into to (';
with 源表数据集 do
begin
for i:=0 to fieldcount-1 do
begin
SqlString:=SqlString+F_GetField(源表数据集控件,i);
if i<fieldcount-1 then SqlString:=SqlString+',' else SqlString:=SqlString+')';
end;
end;
sqlString:=SqlString+' select * from from';
用到的函数如下(如有不解之处,请自行查看datatype或fieldtype的帮助):
Function F_GetField(B_DataSet:TDataSet;B_Zdh:integer;B_Mz:integer=0):String;
//Function F_GetField(B_DataSet:TAdoDataSet;B_Zdh:integer;B_Mz:integer=0):String;
//根据给出的字段号返回该字段的Sql字串,数据表为Set_Ora,字段号从0开始计.B_Mz为0时返回字段名,1时返回字段值
begin
With B_DataSet do
if B_Mz=0 then Result:=fields[B_Zdh].FieldName
else
begin
if (Fields[B_Zdh].DataType =ftWideString) or (Fields[B_zdh].DataType =ftString) then
begin
Result:=iif(fields[B_Zdh].asstring='','null',''''+Fields[B_Zdh].asstring+'''');
exit;
end;
if Fields[B_Zdh].DataType = ftFloat then
begin
Result:=iif(fields[B_Zdh].asstring='','null',floatToStrF(Fields[B_Zdh].AsFloat,ffFixed,11,2));
exit;
end;
if (Fields[B_Zdh].DataType = FtSmallInt) or (Fields[B_Zdh].DataType =ftInteger)
or (Fields[B_Zdh].DataType=FtBCD) then
begin
Result:=iif(fields[B_Zdh].asstring='','null',fields[B_Zdh].AsString);
exit;
end;
if Fields[B_Zdh].DataType=ftDate then
begin
Result:=iif(Fields[B_Zdh].asstring='','null','#'+formatDateTime('yyyy-mm-dd',fields[B_Zdh].asDateTime)+'#');
exit;
end;
Result:=''; //尚需增加布尔等类型的判断.
end;
end;