我做一个值班系统,数据库插入(姓名、日期、值班类型),我要判断(日期,值班类型)是否已经有人插入,我该如何判断?
【假如我select name from.. where date=zdate,type=ztype的话,我该如何判断name非空?】

解决方案 »

  1.   

    if not exists(select xxx from...)
       insert into...
      

  2.   

    用楼上的方法可以,或者select出来后,看看数据集的recordcount是否为0也可以。
      

  3.   

    Query也能用if not exists(select xxx from...)
       insert into...
    如果你不放心就这样
    insert into ...
    where not exists(...)
      

  4.   

    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) 
    第一个存在时返回真
    第二个不存在时返回真
      

  5.   

    请问具体如何做?

    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 吗?
    能给我具体示例吗?谢谢!
      

  6.   

    1楼的方法还有 dulei115方法都可以用在adoquery中。
      

  7.   

    adoquery1.sql.clear;
    adoquery1.sql.add('select name from.. where date=zdate,type=ztype');
    adoquery1.open;
    if adoquery1.recordcount>0 then
      showmessage('已经有人插入记录)
    else 
      showmessage('无记录');
      

  8.   

    adoquery1.close;
    adoquery1.sql.text:=Format('Insert Into ... where not exists(select name from.. where date=''%s'',type=''%s'')',[zdate,ztype]);
    adoquery.execsql;