如:数据库中已有的数据如下
   日期 时间 事件
2011年4月26日 12:12:12 2345
2011年4月26日 12:13:12 2567
2011年4月26日 12:14:12 2342
2011年4月26日 12:15:12 4565
2011年4月26日 12:16:12 7895现在我要插入以下的数据:
   日期 时间 事件
2011年4月26日 12:12:12 2345
2011年4月26日 12:13:12 2567
2011年4月26日 12:14:23 2342
2011年4月26日 12:15:45 4565
2011年4月26日 12:16:68 7895插入数据中前两条数据库中已经有了,我要怎么写SQL语句才能避免再次插入这两条数据呢?

解决方案 »

  1.   

    insert into tb1
     select * from tb2
     execpt
     select * from tb1
      

  2.   

    --用插入前触发器,过滤不需要的数据
    CREATE TRIGGER Ti_tablea ON dbo.table1
    INSTEAD OF INSERT AS
    begin
    insert into table1(日期,时间,事件)
    select 日期,时间,事件
    from inserted i
    where not eixsts(select 1 from table1 where table1.日期= i.日期
    and table1.时间 = i.时间
    and table1.事件 = i.事件)
    end
      

  3.   

    建立唯一索引了
    create table a (id int constraint pk_id unique)
      

  4.   

     ----------------------------------------------------------------------------------
     -- Author : htl258(Tony)
     -- Date   : 2011-04-26 10:43:31
     -- Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
     --          Jul  9 2008 14:43:34 
     --          Copyright (c) 1988-2008 Microsoft Corporation
     --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
     -- Blog   : http://blog.csdn.net/htl258
     ----------------------------------------------------------------------------------
     
     --> 生成测试数据表: [tb]
     IF OBJECT_ID('[tb]') IS NOT NULL
      DROP TABLE [tb]
     GO
     CREATE TABLE [tb] ([日期] [nvarchar](20),[时间] varchar(10),[事件] INT)
     INSERT INTO [tb]
     SELECT '2011年4月26日','12:12:12','2345' UNION ALL
     SELECT '2011年4月26日','12:13:12','2567' UNION ALL
     SELECT '2011年4月26日','12:14:12','2342' UNION ALL
     SELECT '2011年4月26日','12:15:12','4565' UNION ALL
     SELECT '2011年4月26日','12:16:12','7895'
     
     
     -->SQL查询如下: 
     --创建自动过滤重复值索引
     CREATE UNIQUE INDEX IX_tb ON tb([日期],[时间],[事件])
     WITH IGNORE_DUP_KEY 
     --插入数据
    INSERT INTO [tb]
    SELECT '2011年4月26日','12:12:12','2345' UNION ALL
    SELECT '2011年4月26日','12:13:12','2567' UNION ALL
    SELECT '2011年4月26日','12:14:23','2342' UNION ALL
    SELECT '2011年4月26日','12:15:45','4565' UNION ALL
    SELECT '2011年4月26日','12:16:68','7895' 
     
    --查询结果
    SELECT * FROM [tb]
    /*
    日期                   时间         事件
    -------------------- ---------- -----------
    2011年4月26日           12:12:12   2345
    2011年4月26日           12:13:12   2567
    2011年4月26日           12:14:12   2342
    2011年4月26日           12:14:23   2342
    2011年4月26日           12:15:12   4565
    2011年4月26日           12:15:45   4565
    2011年4月26日           12:16:12   7895
    2011年4月26日           12:16:68   7895(8 行受影响) 
    */ 
      

  5.   

    何必这么复杂?begin tran
    insert tb values('2011年4月26日', '12:12:12' ,'2345')
    if(select count(*) from tb where (日期='2011年4月26日') and (时间='12:12:12') and (事件='2345'))>1
    rollback tran
    else 
    commit tran
      

  6.   

    用关键字 distinct就可以了。