Solved

How to copy a row into another row ?

Posted on 2003-11-08
5
405 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get count of orders by customer Sql Server table. 3 48
Search Text in Views 2 28
Group ordersum by orderdate 3 19
SqlServer amend PK column 5 13
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

738 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