adoquery3查询数据循环插入adoquery1里面
adoquery1总共有6个字段'a','b','c','d','e','f',adoquery3先用select h from table,有字段h,怎么把adoquery3查询的数据插入adoquery1的a里面啊,然后b,c,d,e,f字段都为0!求教,谢谢啊
[最优解释]
var
h: string;
begin
with adoquery3 do
begin
close;
sql.clear;
sql.add('select h from table');
open;
while not eof do
begin
h := FieldByName('h').AsString;
with adoquery1 do
begin
close;
sql.clear;
sql.add('insert into table2(a,b,c,d,e,f) values ('''
+ h ''', 0,0,0,0,0)');
ExecSql;
end;
Next;
end;
end;
end;
[其他解释]
你跟踪程序是在执行插入的时候报错吗?如果是看一下数据库字段的类型之类,大小,是否为空等项。
还有就是with这个东西少用,尤其是这种嵌套使用,很有可能出错,你最好是把这个去掉试试!
[其他解释]
begin完整的代码
with adoquery3 do
begin
close;
sql.clear;
adoquery3.SQL.Text:='select distinct '
+'n.remote_id from user_data as m inner join readdata as n on m.'
+'remote_id=n.remote_id and m.selinfo='''+h+''''
+' where m.remote_id not in(select e.remote_id from readdata as e inner join'
+'(select distinct a.xiaoqu_name ,a.remote_id as remote_id,max(b.readtime)'
+' as readtime from user_data as a inner join readdata as b on a.remote_id=b.remote_id'
+' and a.selinfo='''+h+''' and b. readtime>'''+s1+''' and b.readtime < '''+s2+''''
+'group by a.xiaoqu_name,a.remote_id ) as f on e.remote_id=f.remote_id '
+'and e.readtime=f.readtime ) order by n.remote_id' ;
open;
while not eof do
begin
u := FieldByName('remote_id').AsString;
with adoquery1 do
begin
close;
sql.clear;
sql.add('insert into export(id,nf,yf,byds,sgds,bj) values ('''
+ u +''', 0,0,0,0,0)');
ExecSql;
end;
Next;
end;
end;
end;