就一人表A.有字段id,month,tbh,sfbs 
id是自动编号
month是指月份,如200609
month有重复记录
现在想求一条语句,就是取出09月的数据全部取出,然后改成10月份再存到表A中,要求会判断表中有没有10月份数据的存在,month+tbh+sfbs三个字段是唯一的数据...谢谢..

解决方案 »

  1.   

    insert into 表A([month],tbh,sfbs)
    select 
        left(t.[month],4)+'10',
        t.tbh,
        t.sfbs 
    from 
        表A t
    where
        right(t.[month],2)='09'
        and 
        not exists(select 1 from 表A where [month]=left(t.[month],4)+'10' and tbh=t.tbh and sfbs=t.sfbs)
      

  2.   

    --try
    insert into A(month,tbh,sfbs)
    select left(month,4)+'10',tbh,sfbs  from A where right(month,2)='09'
    where not exists (select 1 from A t where left(t.month,4)=left(a.month,4) 
                                              and a.tbh=t.tbh and a.sfbs=t.sfbs)
      

  3.   

    --汗下
    where not exists ----and not exists
      

  4.   

    INSERT INTO 表A
    SELECT ‘200610’,...所有字段
    FORM 表A
    WHERE MONTH = ‘200609’AND 
          (‘200610’+ CAST(tbh AS VARCHAR(100)) +  CAST(sfbs AS VARCHAR(100)) NOT IN    (SELECT MONTH + CAST(tbh AS VARCHAR(100)) + 
          CAST(sfbs AS VARCHAR(100)) FROM 表A)
    以上假设你的 tbh 、sfbs不是字符型,如果是就不用CAST了,没有数据库,不好验证,供参考。
      

  5.   

    用ACCESS数据库,哪一条可以用??
      

  6.   

    insert into 表A([month],tbh,sfbs)
    select 
        left(t.[month],4)+'10',
        t.tbh,
        t.sfbs 
    from 
        表A t
    where
        right(t.[month],2)='09'
        and 
        not exists(select 1 from 表A where [month]=left(t.[month],4)+'10' and tbh=t.tbh and sfbs=t.sfbs)