比如,一共10个字段,第一个字段是年。2011,一共有20个record是关于2011的,
现在想copy这些record但是把2011换成2012,其他field的值不变,请问该如何写。谢谢。

解决方案 »

  1.   

    create table #t
    (
      年 int,
      其它字段 varchar(10)
    ) insert into #t 
    select 2011,'aaa' union all
    select 2011,'bbb'insert into #t select 2012,其它字段 from #t where 年=2011select * from #t
      

  2.   

    按1楼的写法,如果楼主嫌10个字段写起来麻烦的话,可以参考一下这种做法select * into #tt from #t where 年=2011
    update #t set 年=2102
    inset into #t select * from #tt
      

  3.   


    select 2012 as 第一个字段名,* from table where 第一个字段名=2011
      

  4.   

    TABLE1 ---原表
    TABLE2 ---对象表INSERT INTO TABLE2 SELECT 除年字段其他字段, '2012' AS 年 FROM TABLE1 WHERE 年 = '2011'两个表的字段对应上就OK
      

  5.   


    declare @t table 
    (col1 int,col2 int,col3 int,col4 int,col5 int,col6 int,col7 int,col8 int,col9 int,col10 int)
    insert into @t
    select 2011,1,1,1,1,1,1,1,1,1 union all
    select 2011,1,1,1,1,1,1,1,1,1 union all
    select 2010,1,1,1,1,1,1,1,1,1insert into @t
            ( col1 ,
              col2 ,
              col3 ,
              col4 ,
              col5 ,
              col6 ,
              col7 ,
              col8 ,
              col9 ,
              col10
            )
    select   '2012',
              col2 ,
              col3 ,
              col4 ,
              col5 ,
              col6 ,
              col7 ,
              col8 ,
              col9 ,
              col10 from @t where col1=2011
              
    select * from @t
    /*
    col1        col2        col3        col4        col5        col6        col7        col8        col9        col10
    ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
    2011        1           1           1           1           1           1           1           1           1
    2011        1           1           1           1           1           1           1           1           1
    2010        1           1           1           1           1           1           1           1           1
    2012        1           1           1           1           1           1           1           1           1
    2012        1           1           1           1           1           1           1           1           1
    */
      

  6.   

    谢谢回答,不过还是插入原来的表,所有的fields都需要填,所谓无所谓谁是主键。
      

  7.   

    update tb set 年='2012' where 年='2011'
      

  8.   

    INSERT INTO Tabel_Name(Col1,Col2,Col3,……,Col10)
    SELECT '2012' 年, Col2,Col3,……,Col10  FROM Tabel_Name WHERE Col1 = '2011'
      

  9.   

    谢谢,可是好像db2上面运行了一下不行啊。
    我用的是很古老的绿屏系统,只能用一句sql不能这样多句一起的。