表a,有很多很多字段,其中ID为主键,ret为查询条件.
我要做的是根据传来的ret,在表a中查出这条数据,然后再重新插入表a,同时,改变ID的值(因为是主键).
如果没有主键,可以直接写insert into a select * from a where ret='' 可是现在有个主键,怎么办啊?字段非常的多,不可能一个一个写出来insert into values(a,b,c,d,e...)像这样不行的.,..大家帮帮忙..

解决方案 »

  1.   

    insert into a select 'a','b'或者insert into a(字段1,字段2...) select '对应的值1','对应的值2'....
      

  2.   


    declare @s varchar(2000)
    set @s = ''
    select @s = @s + stuff(
    (select ','+[name] from syscolumns 
    where id = object_id('tb') and [name]!='field1'  for xml path('')),1,1,'')
    --print @s
    exec ('select '+@s +' from tb')表tb
    去掉字段field1
      

  3.   


    declare @col nvarchar(2000)
    select @col=isnull(@col,'')+','+b.name from sys.sysobjects a join sys.syscolumns b on a.id=b.id
    where   a.name='sysrowsets'
    select stuff(@col,1,1,'')
    --rowsetid,ownertype,idmajor,idminor,numpart,status,fgidfs,rcrows
      

  4.   

    declare @str varchar(max)
    select @str=stuff((select ','+name from syscolumns where id=object_id('a') and name<>'id' for xml path('')),1,1,'')
    exec('insert into a('+@str+') select '+@str+' from a where ret=''''') 
      

  5.   

    哥哥们..不是这个意思.辛苦你们了.
    例如表a,3个字段,ID,name,age,其中id为主键
    insert into a select * from a where name=''
    这样插入肯定提示id不能插入重复列,我的意思是,怎么在插入的同时,改变id的值
      

  6.   

    insert into a select name,age from a where name=''
      

  7.   

    问题描述清楚就OK,比如5F的方式比标题内容好。除去ID主键字段。
      

  8.   

    比如F8,在数据库中选择该表,右键select保存为查询窗口,去除ID主键,剩下就是您需要的。
      

  9.   

    我是打比方只有3个字段..其实有很多..
    insert into a select name,age from a where name=''这个...我压力很大...
      

  10.   


    不管你有多少
    declare @s varchar(2000)
    set @s = ''
    select @s = @s + stuff(
    (select ','+[name] from syscolumns 
    where id = object_id('a') and [name]!='id'  for xml path('')),1,1,'')
    --print @s
    exec ('select '+@s +' from a')---可以先通过这个看效果。
    都能查出来,tb换成你的表名称 field1换成id(也就是你要排查的列)id是不是自增的?自增的就不用指定了exec('insert into a select '+@s +' from a where ret =''条件值''')