Solved

How to copy a row into another row ?

Posted on 2003-11-08
5
391 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:marhk51
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
arbert earned 20 total points
ID: 9707988
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
 

Author Comment

by:marhk51
ID: 9708238
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
 
LVL 34

Expert Comment

by:arbert
ID: 9708568
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
 
LVL 1

Expert Comment

by:rhodeisland911
ID: 9713273
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
 

Author Comment

by:marhk51
ID: 9720901
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now