Solved

How to copy a row into another row ?

Posted on 2003-11-08
5
401 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

792 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