表是采用 a,b 双主键的 其余的字段是动态生成的。
现在知道 a,b的值。要插入一条除了b增加1之外完全相同的数据,请问sql语句该怎么写?insert   into   表(字段1,字段2,字段3)   select   字段1,字段2,字段3   from   表   where   字段1=
这种方法貌似是不好用的 因为字段3之后都是自动生成的

解决方案 »

  1.   

    想偷懒的话用临时表。select * into #temp from 表;
    update #temp set b = b+1insert into 表 select * from #temp 
      

  2.   

    --试试declare @strinsert varchar(1000)
    declare @strSelect varchar(1000)
    select @strinsert='b'
    declare @strSelect='b+1'
    select @strinsert=isnull(@strinsert',','')+name,@strSelect=isnull(@strSelect',','')+name 
    from syscolumns 
    where [name]<>'a' and [name]<>'b' and id=object_id('tb')
    print('insert into tb('+@strinsert+') select '+@strSelect+' from tb ')
    exec('insert into tb('+@strinsert+') select '+@strSelect+' from tb ')
      

  3.   

     [code=SQL]--改了下 试试declare @strinsert varchar(1000)
    declare @strSelect varchar(1000)
    select @strinsert='b'
    select @strSelect='b+1'
    select @strinsert=isnull(@strinsert+',','')+name,@strSelect=isnull(@strSelect+',','')+name 
    from syscolumns 
    where [name]<>'a' and [name]<>'b' and id=object_id('tb')
    print('insert into tb('+@strinsert+') select '+@strSelect+' from tb ')
    exec('insert into tb('+@strinsert+') select '+@strSelect+' from tb ')
    [/code]
      

  4.   


    insert into 表
    select a,b+1,c from 表 
      

  5.   

    SQL code
    insert into 表
    select a,b from 表 
      

  6.   


    --带条件
    insert into 表
    select a,b+1,c from 表 where 字段1=
      

  7.   

    慕白兄的临时表可以用 但是好像只能用一次 以后就提示数据库中已存在名为 '#temp1' 的对象。mail_ylei 的方法 插入错误: 列名或所提供值的数目与表定义不匹配。
      

  8.   

    慕白兄的临时表可以用 但是好像只能用一次 以后就提示数据库中已存在名为 '#temp1' 的对象。
    ===>连接关闭后就没有了,如果你要在一个连接中用多次,可加一句
    drop table #temp
      

  9.   

    select * into #temp from tb8 where DataID ='tb8a5587164-8ad8-4aa9-94ea-2488b82b582c'AND StepInstID='4'
    update #temp set StepInstID = StepInstID+1insert into tb8 select * from #temp where DataID ='tb8a5587164-8ad8-4aa9-94ea-2488b82b582c'AND StepInstID='5';
    drop table #temp还是提示 数据库中已存在名为 '#temp' 的对象。
      

  10.   

    还是提示 数据库中已存在名为 '#temp' 的对象。
    你先把连接断开,再测试。
      

  11.   

    就是动态sql,因为你的列式动态的你也不知道列名
      

  12.   

    恐惧之狼的方法 是可行的。
    declare  @Columns nvarchar(4000)
    declare  @FinalSql nvarchar(4000)
    select @Columns=isnull(@Columns, ' ')+ '['+name+ '] ,'   from   syscolumns   where   id=object_id( 'tb8 ') 
      and   name!= 'DataID' and   name!= 'StepInstID'
    set   @Columns=left(@Columns,len(@Columns)-1)set @FinalSql = 'insert into tb8(DataID, StepInstID,' + @Columns + ') 
    select DataID, StepInstID + 1, ' + @Columns + ' from tb8 where DataID =''tb8a5587164-8ad8-4aa9-94ea-2488b82b582c''AND StepInstID=''6''' 
    print @FinalSqlexec (@FinalSql)