How to copy a row into another row ?

Hi,

Is there a quick way to copy a complete row of data (by using the row ID) into a new row so it is given a new ID.

I can't think of any other way than, copying all of the data into variables and then inserting it back into the DB.

What I need is an exact copy but just different ID numbers.

BTW, I can't just CHANGE the ID numbers as they are generated automaticaly every time a new entry is entered into the DB.

Any Ideas ???

Thanks.
marhk51Asked:
Who is Participating?
 
arbertCommented:
is the "id number" and identity field?

You can just use and insert into statement.

Insert into table (id, value1,value2,value3)
select yournewidhere, column1,column2,column3  from yourtable
where id=oldid

Brett
0
 
marhk51Author Commented:
No, I don't think you get what I mean.

I know how to insert things into a table...

I have one row in a table which has a load of columns, so copying each individiual field and re-inserting them into a new row would take too long.

So is there anyway I can COPY a existing row and create a new row with the same data, but obviously a new ID number.

The ID number which I refer to, is the unique identity number given to each row as it is created.

Hope this makes things more clear.

Thanks.
0
 
arbertCommented:
Ya, I thought I understood.  You want to copy the row (create a new one with a new id and keep the existing row).  YOURNEWIDHERE is the ID number that you want the new ID to be...

If the id number is an identity field, you would have to perform a

set idenitity_insert yourtablenamehere ON

Insert into table (id, value1,value2,value3)
select yournewidhere, column1,column2,column3  from yourtable
where id=oldid

set identity_inser yourtablenamehere OFF
0
 
rhodeisland911Commented:
I think what arbert states is right, but you should leave out the id field on the insert:

Insert Into [TableName](fieldx, fieldy, fieldz)
Select fieldx, fieldy, fieldz From [TableName] Where id = x

Good Luck
0
 
marhk51Author Commented:
Thanks for your comments guys, I gotit to work just they way I wanted it. It was a bit tedious entering in all the column names thou'

Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.