用变量保存字段名,先判断,未选择就是空值 var fName:string;fName:=Trim(ComboBox2.text); if fName='' then fName:='null'; //如果字段不允许是null,就改成fname='''';...select('+ComboBox1.Text+' , '+fName+' , '+ComboBox3.text+') from '+oldtable;
SQL语句有错insert into newtable(D,E,F)中字段列的数目必须与你选出来的一致 用动态字段列表就可以了 var OldFieldList,NewFieldList : string; begin OldFieldList := ''; NewFieldList := ''; if Trim(ComboBox1.text) <> '' then begin OldFieldList := OldFieldList + ',' + Trim(ComboBox1.text); NewFieldList := NewFieldList + ',D'; end; if Trim(ComboBox2.text) <> '' then begin OldFieldList := OldFieldList + ',' + Trim(ComboBox2.text); NewFieldList := NewFieldList + ',E'; end; if Trim(ComboBox3.text) <> '' then begin OldFieldList := OldFieldList + ',' + Trim(ComboBox3.text); NewFieldList := NewFieldList + ',F'; end; if OldFieldList <> '' then begin OldFieldList := Copy(OldFieldList,2,MaxInt); NewFieldList := Copy(NewFieldList,2,MaxInt); end; //拼接SQL语句 SQLStr := 'insert into newtable(' + NewFieldList + ') select ' + OldFieldList + ' from'+oldtable; …………………… end;
var fName:string;fName:=Trim(ComboBox2.text);
if fName='' then
fName:='null'; //如果字段不允许是null,就改成fname='''';...select('+ComboBox1.Text+' , '+fName+' , '+ComboBox3.text+') from '+oldtable;
用动态字段列表就可以了
var
OldFieldList,NewFieldList : string;
begin
OldFieldList := '';
NewFieldList := '';
if Trim(ComboBox1.text) <> '' then begin
OldFieldList := OldFieldList + ',' + Trim(ComboBox1.text);
NewFieldList := NewFieldList + ',D';
end;
if Trim(ComboBox2.text) <> '' then begin
OldFieldList := OldFieldList + ',' + Trim(ComboBox2.text);
NewFieldList := NewFieldList + ',E';
end;
if Trim(ComboBox3.text) <> '' then begin
OldFieldList := OldFieldList + ',' + Trim(ComboBox3.text);
NewFieldList := NewFieldList + ',F';
end; if OldFieldList <> '' then begin
OldFieldList := Copy(OldFieldList,2,MaxInt);
NewFieldList := Copy(NewFieldList,2,MaxInt);
end;
//拼接SQL语句
SQLStr := 'insert into newtable(' + NewFieldList + ') select '
+ OldFieldList + ' from'+oldtable;
……………………
end;