修改某记录,记录主键可修改.保存时怎样判断是否重复?

解决方案 »

  1.   

    先用select语句查询该主键是否存在
      

  2.   

    to zhymax:
      这里是修改啊,而不是新增.
      

  3.   

    使用刻龍:
      qryTemp:=TADOQuery.Create(nil);
      qryTemp.connection:=datamodule.cn;
      qryTemp.Clone(qrySource,ltBatchOptimistic);
    然後在qryTemp裡面查找edt.text,接著判斷recno是否與qrySource的相同
      

  4.   

    to Suton:
      qryTemp.Clone(qrySource,ltBatchOptimistic);
      此行什么意思啊.能详细点吗?
      

  5.   

    简单方法
    try
    //修改语句
    except
    //修改不成功弹出异常
    end;
      

  6.   

    qryTemp.Clone(qrySource,ltBatchOptimistic);
    从qrySource拷贝数据到qryTemp
      

  7.   

    to  easysm (奔三) :
    你将修改后的主键值作为参数进行查询,
    select count(*) from table where 主键=:pid
    如果大于等于1,那就不行。(主键值应该和以前不同,否则根本就不执行这个判断)
      

  8.   

    同意 caijitao(阿修罗),
    也就是if newvalue<>oldvalue then
    select count(*) from table where 主键=:pid
      

  9.   


     to:caijitao(阿修罗)
     如果主键值没有修改,保存时就不行了.怎样判断主键值是否改变啊. to :plq(不懂就问) 
      你的newvalue 与oldvalue 何来?
      

  10.   

    在你修改的table中加入trigger :
    Create Trigger TU_table on table_a 
    for update]
    as 
    If exists (select * from table_a a, Inserted  b
               where a.key=b.key
               )
    Begin
       Raiserror 333333 '鍵值重複'
       rollback transaction
    End
      

  11.   

    或者在 table.beforepost時間中判斷是否已經存在該鍵值If 存在
      table.cancel
    else 
      post
      

  12.   

    to easysm(奔三) :
    不好意思我打错了,我的思路是
    首先保存住原来的pid,然后在
    你要提交时(比如某个确定按钮)
    与新的pid进行比对,如果还相等,则
    不做任何动作,如果不相等,则使用
    一个临时的query查找是否已有pid与新的
    pid相冲突,如果有则提示
      

  13.   

    假如修改前主鍵為Str1,改后的主鍵為Str2
    str1:=Adoquery1.fieldbyname('主鍵').value;//Str1:=DBEdit1.text;
    Adoquery1.edit;
    Str2:=DBedit1.text;
    if Str1<>Str2 then 
    begin
    adoquery.close
    adoquery.SQL.add('secect 主鍵 from table1 where 主鍵='+#39+Str+#39);
    adoquery.open;
    if  adoquery.recordcount>=1 then
      showmeeage('主鍵存在');
    adoquery.close;
    end;
    adoquery1.post;