往数据库添加信息时,怎样判断使不出现重复的信息呢??又要怎么实现呢??

解决方案 »

  1.   

    最简单的方式是设计唯一健索引。或者,可以在插入之前先查询,没有的话insert,存在的话update
    譬如,不允许字段f1出现重复.则可以采用如下更新方式.
    if exists(select 1 from t1 where f1 = 'f1')
    update t1 set f2 = 'f2' where f1 = 'f1'
    else 
    insert t1(f1,f2) values ('f1','f2')
      

  2.   

    if ADOQ_Company.State = dsinsert then
            begin
              with ADOQuery1 do
                begin
                  Close;
                  SQL.Clear;
                  SQL.Add('select sh from Company where sh =:a');
                  Parameters.ParamByName('a').Value := s;
                  Open;
                end;
                if not ADOQuery1.IsEmpty then
                begin
                  showMessage('XX不能重复,请重新输入');
                  DBEdit_sh.SetFocus;
                  Exit;
                end;
              ADOQuery1.Close;
            end;