……  from AAA_test.dbo.Emp_BaseInfo where 身份证ID not in (select 身份证ID from AAA_goodtese.dbo.Emp_BaseInfo where 身份证ID is not null)上面的语句是判断2个表内的身份证不能重复,对吗?( 如果不是,怎么写SQL语句,不让AAA_test内的数据重复插入AAA_goodtese内(因为只有身份证唯一,AAA_test表内的数据都是收集起来的,难免有错误,所以就用身份证号判断不让AAA_goodtese表内已有的数据重复添加~) 添加不成功的数据能倒出来吗?怎么写?)还判断了不许为空?各位帮帮忙 解答一下~------------------------------------------------
可以不看上面的啰嗦话谁能帮我写一个SQL语句需求:表A与表B字段全部一样,但是表A是收集来的数据,表B是实际使用的数据,我要从表A往表B导入数据,我要判断如果表B内的数据如果已经有了(用身份证判断),表A就不向表B插入这条数据,并且把没有插入成功的数据导出来

解决方案 »

  1.   

    上面的sql语句是查询:身份证ID为Null的
      

  2.   

    SELECT * INTO B FROM 
    (SELECT A.* FROM A WHERE NOT EXISTS( SELECT 1 FROM B WHERE A.ID = B.ID))
      

  3.   

    insert AAA_goodtese.dbo.Emp_BaseInfo
    select * from AAA_test.dbo.Emp_BaseInfo 
    where 身份证ID is not null 
    and 身份证ID not in (select 身份证ID from AAA_goodtese.dbo.Emp_BaseInfo)
    身份证ID为空的怎么处理
      

  4.   

    select 身份证ID from AAA_goodtese.dbo.Emp_BaseInfo where 身份证ID is not null
    查询非空身份证idfrom AAA_test.dbo.Emp_BaseInfo where 身份证ID not in (
    从AAA_test.dbo.Emp_BaseInfo表中查询身份证id不在由 (表AAA_goodtese.dbo.Emp_BaseInfo中非空身份证id)构成的集合中。
    from AAA_test.dbo.Emp_BaseInfo a where not exists (select 1 from AAA_goodtese.dbo.Emp_BaseInfo b where a.身份证ID ==b.身份证id)
      

  5.   

    insert into B select *  from A  where 身份证ID not in (select 身份证ID from B)
      

  6.   

    if object_id('tempdb.dbo.#a') is not null drop table #a
    go
    if object_id('tempdb.dbo.#b') is not null drop table #b
    go
    create table #a(id int,col varchar(10))
    insert into #a select 1,'a'
    union all select 2,'b'
    union all select 3,'c'
    union all select 4,'d'create table #b(id int,col varchar(10))
    insert into #b select 1,'a'
    union all select 2,'b'
    union all select 3,'c'select * from #a
    select * from #binsert into #b select id,col from #a a where not exists(select 1 from #b b where a.id=b.id )
      

  7.   

    insert into B select * from A where 身份证ID not in (select 身份证ID from B)
    如果A中空的数据也不导过去的话,那么加上
    and 身份证ID is not null
      

  8.   

    [没插入成功的怎么显示出来??Quote=引用 5 楼 xuam 的回复:]
    insert into B select * from A where 身份证ID not in (select 身份证ID from B)
    [/Quote]