大家帮我解决个问题啊,delphi相关数据库问题我现在数据库里有两个表如:DBJ09和DJ09,数据样本如下
BDJ09I01 I07 I40
000101 出让 贷款
000102 转让 现金
000103 出让 现金DJ09 的字段和BDJ09的字段相同,但是数据为空我现在要将BDJ09里的数据全部一气添加到DJ09里,以I01为对应关系,并且一按钮的方式执行。请大家帮帮我的忙吧,谢谢谢谢,请将完整程序及相关代码编写后跟帖~~
BDJ09I01 I07 I40
000101 出让 贷款
000102 转让 现金
000103 出让 现金DJ09 的字段和BDJ09的字段相同,但是数据为空我现在要将BDJ09里的数据全部一气添加到DJ09里,以I01为对应关系,并且一按钮的方式执行。请大家帮帮我的忙吧,谢谢谢谢,请将完整程序及相关代码编写后跟帖~~
procedure TForm1.Button1Click(Sender: TObject);
begin
ADOCommand1.CommandText:='insert into DJ09 select * from BDJ09';
ADOCommand1.Execute;
end;
begin
with Query1 do begin
Close;
SQL.Clear;
SQL.Text:='Insert into DJ09 select * from BDJ09 where I01 not in (select I01 from DJ09)';
ExecSQL;
end;
end;
//我喜歡用adoquery
var
str:string;
begin
str:='insert into DJ09 select * from BDJ09 A where not exists(select 1 from DJ09 where A.I01=I01)';
with adoquery do
begin
close;
sql.clear;
sql.add(str);
execsql;
if rowsaffected>0 then
application.messagebox('保存成功!','提示',mb_okcancel)
else
application.messagebox('','',mb_okcancel);
end;end;
建議樓主可以在數據庫中建一個觸發器來搞定。當然在程序中也行
create trigger del_BDJO9 on DJ09
after insert
as
begin
delete BDJO9
where I01 in (select I01 from inserted)
end
1、运行效果图2、设计界面图
3、代码如下:
//查询DJ09表数据
procedure TForm1.Button2Click(Sender: TObject);
begin
ADODataSet2.Close;
ADODataSet2.CommandText := 'select * from DJ09 ';
ADODataSet2.Open;
end;
//导入表DJ09
procedure TForm1.Button1Click(Sender: TObject);
var
Str:string;
begin
with ADODataSet1 do
begin
First;
while not Eof do
begin
adoTmp.Close;
adoTmp.SQL.Clear;
adoTmp.SQL.Add('insert into DJ09 values('''+FieldValues['I01']+''','+
''''+FieldValues['I07']+''','+
''''+FieldValues['I40']+''' )');
adoTmp.ExecSQL;
Next;
end;
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ADODataSet1.Close;
ADODataSet1.CommandText := 'select * from BDJ09';
ADODataSet1.Open;
end;如有问题,欢迎继续切磋!
begin
ADOCommand1.CommandText:='insert into DJ09 select * from BDJ09';
ADOCommand1.Execute;
ADOCommand1.CommandText:='delete from BDJ09';
ADOCommand1.Execute;
end;
本地库://查询DJ09表数据
procedure TForm1.Button2Click(Sender: TObject);
begin
ADODataSet2.Close;
ADODataSet2.CommandText := 'select * from DJ09 ';
ADODataSet2.Open;
end;
//导入表DJ09
procedure TForm1.Button1Click(Sender: TObject);
var
Str:string;
begin
with ADODataSet1 do
begin
First;
while not Eof do
begin
adoTmp.Close;
adoTmp.SQL.Clear;
adoTmp.SQL.Add('insert into DJ09 values('''+FieldValues['I01']+''','+
''''+FieldValues['I07']+''','+
''''+FieldValues['I40']+''' )');
adoTmp.ExecSQL;
Next;
end;
end;
end;
网络库:procedure TForm1.Button1Click(Sender: TObject);
begin
with Query1 do begin
Close;
SQL.Clear;
SQL.Text:='Insert into DJ09 select * from BDJ09 where I01 not in (select I01 from DJ09)';
ExecSQL;
end;
end;