Query也能用if not exists(select xxx from...) insert into... 如果你不放心就这样 insert into ... where not exists(...)
select count(name)c1 from.. where date=zdate,type=ztype记录集会返回一个整数,>0表示存在几条,=0表示不存在,c1 是别名。 或者: exists(select name from.. where date=zdate,type=ztype) not exists(select name from.. where date=zdate,type=ztype) 第一个存在时返回真 第二个不存在时返回真
请问具体如何做? 是 str:='select name from zhiban where date='''+zdate+''',type='''+ztype+''' '; adoquery1.Close; adoquery1.SQL.Clear; adoquery1.SQL.Add(str); if not exists(adoquery1.ExecSQL) insert into 吗? 能给我具体示例吗?谢谢!
1楼的方法还有 dulei115方法都可以用在adoquery中。
adoquery1.sql.clear; adoquery1.sql.add('select name from.. where date=zdate,type=ztype'); adoquery1.open; if adoquery1.recordcount>0 then showmessage('已经有人插入记录) else showmessage('无记录');
adoquery1.close; adoquery1.sql.text:=Format('Insert Into ... where not exists(select name from.. where date=''%s'',type=''%s'')',[zdate,ztype]); adoquery.execsql;
insert into...
insert into...
如果你不放心就这样
insert into ...
where not exists(...)
或者:
exists(select name from.. where date=zdate,type=ztype)
not exists(select name from.. where date=zdate,type=ztype)
第一个存在时返回真
第二个不存在时返回真
是
str:='select name from zhiban where date='''+zdate+''',type='''+ztype+''' ';
adoquery1.Close;
adoquery1.SQL.Clear;
adoquery1.SQL.Add(str);
if not exists(adoquery1.ExecSQL)
insert into 吗?
能给我具体示例吗?谢谢!
adoquery1.sql.add('select name from.. where date=zdate,type=ztype');
adoquery1.open;
if adoquery1.recordcount>0 then
showmessage('已经有人插入记录)
else
showmessage('无记录');
adoquery1.sql.text:=Format('Insert Into ... where not exists(select name from.. where date=''%s'',type=''%s'')',[zdate,ztype]);
adoquery.execsql;