Solved

How to copy a row into another row ?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

831 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