将数据插入表之前,判断表里是否有重复数据,
有则不插入,没有则插入,
如何在一句SQL里完成?

解决方案 »

  1.   

    if not exists(select 1 from tb where id = @id)
        insert into tb values...........
      

  2.   

    good good study,day day up
      

  3.   

    if not exists (select * from tb where ID=@ID)
    insert
    esle 
      

  4.   

    declare @id varchar(10)
    set @id='TEMP_ID' --此表ID或都能唯一标示这一行的那个列名
    if not exists (select 1 from 你要插入表的表名 where 唯一列名=@id)
    print 'No'
    else 
    print '这里写插入操作SQL语句' 
    因为你只是确认有这行数据没有 用 select 1 效率是最高的
      

  5.   

    insert into tb (id) select @id where not exists (select 1 from tb where id=@id)
      

  6.   

    insert into table (ID) select ID = '" + TxtID.Text + "' where not exists (select 1 from table where id = '" + TxtID.Text+ "') 
      

  7.   

    if noexists (select * from table1 where mid='aa') 
    INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc') 
      

  8.   

    这个思路倒不错嘛!判断插入更新都在一个SQL里!