mysql - SQL clone record with a unique index -


is there clean way of cloning record in sql has index(auto increment). want clone fields except index. have enumerate every field, , use in insert select, , rather not explicitly list of fields, may change on time.

not unless want dynamic sql. since wrote "clean", i'll assume not.

edit: since asked dynamic sql example, i'll take stab @ it. i'm not connected databases @ moment, off top of head , need revision. captures spirit of things:

-- list of columns in table select #t exec sp_columns @table_name = n'targettable'  -- create comma-delimited string excluding identity column declare @cols varchar(max) select @cols = coalesce(@cols+',' ,'') + column_name #t column_name <> 'id'  -- construct dynamic sql statement declare @sql varchar(max) set @sql = 'insert targettable (' + @cols + ') ' +     'select ' + @cols + ' targettable somecondition'  print @sql -- debugging exec(@sql) 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -